简体   繁体   中英

REST API call using nodejs and mongodb

I am new to node.js. Following is my GET request:

router.get('/EPODS/Product/:id?', function(req, res) {
  if(req.params.id){
     var inputIdObj={'ProductEBM.DataArea.Product.ProductGroupID': req.params.id};
  } else {
    res.status(500).json({
      "Error": "No input given Try give something or Check for the case sensitive in the input query fields"
    });
    res.end();
    return;
  }

  db.collection('Product').findOne(inputIdObj,function(err, doc) {
      if (err) {
        console.log("No record found for the given input");
        res.end("No record found for the input data- Please check again");
      } else {  
       res.send(doc);  
      }
    });
  });

I am receiving null value when I hit the request in POSTMAN but the query works in MONGODB.

Please Help! Thanks in Advance.

When you get the parameter of the GET request, you get the ID in a String format, which will not work if you use ObjectID. First, cast your param in ObjectID (you can import the object with require('mongodb').ObjectID ) and so create: new ObjectID(req.params.id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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