Javascript 在forEach循环中运行mongoose保存查询
我的graphql解析器中有以下查询:Javascript 在forEach循环中运行mongoose保存查询,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我的graphql解析器中有以下查询: setFeatured:async函数({id},req){ const post=wait post.findById(id) const posts=等待Post.where(“'u id').ne(id) post.featured=true 等待post.save() posts.forEach(pst=>{ pst.featured=false 等待post.save() }) 返回真值 }您还需要将异步设置为传递给forEach的回调 pos
setFeatured:async函数({id},req){
const post=wait post.findById(id)
const posts=等待Post.where(“'u id').ne(id)
post.featured=true
等待post.save()
posts.forEach(pst=>{
pst.featured=false
等待post.save()
})
返回真值
}
您还需要将异步设置为传递给forEach的回调
posts.forEach(异步pst=>{
pst.featured=false
等待post.save()
})
另一种不同的方法是,如果在任何给定的时间都有一个单一的特色文章,您可以将特色字段与文章分开,并有一个记录来保存文章id