为什么我的异步函数返回一个空数组
为什么我的异步函数返回一个空数组
因此,我试图通过将用户的匹配项推入数组并返回该数组来获取用户的匹配项,以便我的路由器可以将数据发送至前端。但是我的异步函数有问题,我只是有一个空数组。我曾尝试设置一些断点,但在我的服务将数据推送到阵列之前,我注意到路由器已发送数据。
有我的路由器代码:
router.get("/allMatchs", auth, async (req, res) => {
const user = await userService.getUserById(req);
const matchs = await service.getMatchsByUser(user);
res.send(matchs);
});
并且有我的服务代码:
async function getMatchsByUser(user) {
const userMatchs = user.matchs;
let matchs;
await userMatchs.map(async (m) => {
let match = await Match.findById(m._id).select([
"-isConfirmed",
"-isUnmatched",
]);
matchs.push(match);
});
return matchs;
}
谢谢您的帮助。
回答如下:您的问题是userMatchs.map
将异步/等待与map()结合使用可能会有些棘手
How to use Async and Await with Array.prototype.map()
最新文章
- CNN中卷积和池化计算公式
- 认证模式之Digest模式
- Annotation 介绍
- snmp编程
- ==时间复杂度==
- MindSpore实现语音指令识别(迁移tf入门教程)
- Platform机制
- includes的使用
- dump文件,windbg
- mingw+msys windows下配置
- 数字化原理
- python,jsonschema, marshmallow, “None is not of type ‘string‘
- 电脑上怎么发出电子版准考证
- SVM算法实现(一)
- STM32F4
- C语言模拟实现:atoi函数
- 【区块链
- (十四)STM32——外部中断(EXTI)
- gets函数用法
- 通讯录——C语言实现