如何解决mongodb中无法通过filter()获取数据
我正在尝试通过此查找分配给用户的任务
router.get('/all',auth,async (req,res) => {
try {
const assignments_raw = await Assignment.find({listP: listP.indexOf(req.user.userId)})
res.json(assignments_raw)
} catch (e) {
res.status(500).json({ message: 'Something went wrong,try again' })
}
})
具体来说,这一行应该已经找到了所有在 listP 字段中具有与用户 ID 对应的元素的任务
const assignments_raw = await Assignment.find({listP: listP.indexOf(req.user.userId)})
但这会导致错误,为什么? 下面是摘自芒果
解决方法
你可以这样做
router.get('/all',auth,async (req,res) => {
try {
const assignments_raw = await Assignment.find()
let assignments = []
assignments_raw.map(assignment => {
if (assignment.listP.indexOf(req.user.userId) !== -1) {
assignments.push(assignment)
}
}
)
res.json(assignments)
} catch (e) {
res.status(500).json({ message: 'Something went wrong,try again' })
}
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。