0

I got a filed "tags" and it has value ["tag1", "tag2", ...] and I got a tag named "tag1" from req.body.tag.

I want to find a tag where tags[...].name = "tag1" how?

this is what I tried

  api.post('/findByTag', async(req, res) => {
    if(!req.body.tag || req.body.tag === 'string') return

    let tag = Array.from(req.body.tag)

    try {
      let memos = await Memo.find({})
      .where('tags')
      .in(tag)
      .limit(30)

      res.status(200).json({ data: memos })
    } catch(err) {
      logger.error(err.message, err)
      res.status(500).json({ message: err.message })
    }
  })
JillAndMe
  • 3,989
  • 4
  • 30
  • 57

1 Answers1

1

As tags contains a simple array of strings you can do the query in this way

api.post('/findByTag', async(req, res) => {
  if(!req.body.tag || req.body.tag === 'string') return

  let tag = Array.from(req.body.tag)

  try {
    let memos = await Memo.find({ tags: tag })
    .limit(30)

    res.status(200).json({ data: memos })
  } catch(err) {
    logger.error(err.message, err)
    res.status(500).json({ message: err.message })
  }
})
PaulShovan
  • 2,140
  • 1
  • 13
  • 22