我如何循环遍历文档列表并使用异步等待在猫鼬中对每个文档执行命令

问题描述 投票:0回答:0

我有用户、帖子和关注集。我想选择帖子,将其限制为 10,并在关注集合中查看他们的作者,无论用户是否关注他。使用异步和等待也让我感到困惑。我试过这段代码

 async function index (req,res){
   const {user_id} = req.body
   const post  =  await Post.find().exec( async (err,doc)=>{
   const isFollowing = await Follows.find({who_id : user_id , whom_id : doc._id })
   if (isFollowing.length > 0){
    return doc
   }
 })

然后我从 VS Code 中发现,如果我使用 exec 函数“'await' 对这个表达式的类型没有影响。

mongodb express mongoose exec
© www.soinside.com 2019 - 2024. All rights reserved.