【发布时间】:2021-01-30 20:54:26
【问题描述】:
我的 graphql 解析器有以下查询:
setFeatured: async function ({ id }, req) {
const post = await Post.findById(id)
const posts = await Post.where('_id').ne(id)
post.featured = true
await post.save()
posts.forEach(pst => {
pst.featured = false
await post.save()
})
return true
}
使用上面的代码 sn -p 我想将用户未选择的帖子的状态设置为 false ,反之亦然。
但我在控制台中收到此错误
SyntaxError: await 仅在异步函数中有效
指向 forEach 循环内的await post.save() 行。
如何为每个帖子运行 save() 查询?
【问题讨论】:
标签: javascript node.js mongodb mongoose