如何才能等待异步?而为什么等待是行不通的?

时间:2020-01-12 16:32:09

标签: javascript node.js asynchronous

这是我的路由器的一部分:

        if (limit <= 100) {
        let id = jwt.verify(req.cookies.tokenlogin, config.get('jwtSecret')).userId
        const results = await User.paginate({ $and: [{ _id: { $ne: id }, fullname: { $regex: search, $options: '-i' } }] },
            { page: page, limit: limit, customLabels: myCustomLabels, lean: true, select: '-email -password' });
        let paginator = results.paginator;
        let arrayRes = results.items;
        await arrayRes.forEach(async (current, i) => {
            await User.findOne({ _id: id }, async (er, rs) => {
                current['followed'] = await rs.followings.includes(current.id);
            })
        })
        res.status(200).json({ items: arrayRes, paginator: paginator })
    }

我使用了await,但是它不起作用,服务器总是将未更新的数组发送给客户端。如何在响应中发送修改后的数组?

0 个答案:

没有答案