簡體   English   中英

無法通過 mongodb 中的過濾器 () 獲取數據

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

我試圖通過這個找到分配給用戶的任務

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM