简体   繁体   English

无法通过 mongodb 中的过滤器 () 获取数据

[英]Can't get data via filter () in mongodb

I am trying to find the tasks assigned to the user through this我试图通过这个找到分配给用户的任务

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' })
  }
})

Specifically, this line should have found all the tasks that have an element corresponding to the user ID inside the listP field具体来说,这一行应该找到了所有在 listP 字段中具有与用户 ID 对应的元素的任务

 const assignments_raw = await Assignment.find({listP: listP.indexOf(req.user.userId)})

But this causes an error, why?但这会导致错误,为什么? below is an excerpt from Mango以下是芒果的摘录关注

You can do like this你可以这样做

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' })
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM