Bryan
Bryan

Reputation: 141

Mongo find id with req.query.id node

Im trying to get the id from the url with req.query.id(I've also used req.body)but it return an empty array but if I put the variable with number one I get the array with data.`

router.get('/:id', isLoggedIn, function(req, res) {
  MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    let id = req.query.id;
    db.collection("restaurant").find( { _id : id } ).toArray(function (err, result) {
      console.log(result);
      res.render('menu', {  restaurant: result });
});
  });
});

`

Upvotes: 2

Views: 1515

Answers (1)

0.sh
0.sh

Reputation: 2762

req.query is use to get the querystring part of the url after the ? querystrings. What you should use in getting the value of id is req.params

const { id } = req.params;

and also you have to convert id to a number your mongodb match query will be

{ _id: Number(id) }

Upvotes: 1

Related Questions