0

This works, but once I change the 123 to req.params.userId, I obtain an empty array from the GET request.

router.get("/:userId", async (req, res) => {
  const posts = await loadPostCollection();
  console.log(req.params.userId) //>>>123
  const info = await posts
    .find({
      userInfo: {
        userId: 123, //req.params.userId doesn't work
        notes: []
      }
    })
    .toArray();
  res.send(await info);
});

edit: the question was not how to convert string to num or num to string, but rather help with finding out what's wrong with the code. Therefore, not duplicate.

Chen W
  • 119
  • 2
  • 12

2 Answers2

0

Oh I got it, req.params.userId is a string while userId is expecting a number.

Chen W
  • 119
  • 2
  • 12
0

It's because of string and number. Try converting the req.params.userId to number and it should work!

Rachit
  • 208
  • 4
  • 19