簡體   English   中英

使用Node.js和MongoDB進行REST API調用

[英]REST API call using nodejs and mongodb

我是node.js的新手。 以下是我的GET請求:

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

我在POSTMAN中命中請求時收到null值,但查詢在MONGODB中有效。

請幫忙! 提前致謝。

當您獲取GET請求的參數時,您會以字符串格式獲得ID,如果您使用ObjectID,該格式將無效。 首先,將參數轉換為ObjectID(可以使用require('mongodb').ObjectID導入對象),然后創建: new ObjectID(req.params.id)

暫無
暫無

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

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