簡體   English   中英

NodeJS Rest Services將參數傳遞給$ near查詢MongoDB

[英]NodeJS Rest Services passing parameter to $near query MongoDB

我已經用NodeJS和MongoDB開發了Rest Services列表。 這些服務之一執行$ near mongodb查詢,以按特定位置將所有元素檢索到特定范圍內

 router.route("/api/geo_cars")
.get(function(req,res){
    var max = 1000;
    var response = {};
    mongoOOp.find({
 location:
   { $near :
      {
        $geometry: { type: "Point",  coordinates: [ 12.560945, 41.957482 ] },
        $maxDistance: max
      }
   }
},function(err,data){
    // Mongo command to fetch all data from collection.
        if(err) {
            response = {"error" : true,"message" : "Error fetching data"};
        } else {
            response = {"error" : false,"message" : data};
        }
        res.json(response);
    });
});

該服務是一項獲取服務,效果很好。 現在我想傳遞給$ near查詢maxDistance值並按Service進行坐標,而我只用這種方式嘗試了maxDistance:

router.route("/api/geo_cars/:maxDistance")
.get(function(req,res){
    var max = req.params.maxDistance;
    var response = {};
    mongoOOp.find({
 location:
   { $near :
      {
        $geometry: { type: "Point",  coordinates: [ 12.560945, 41.957482 ] },
        $maxDistance: max
      }
   }
},function(err,data){
    // Mongo command to fetch all data from collection.
        if(err) {
            response = {"error" : true,"message" : "Error fetching data"};
        } else {
            response = {"error" : false,"message" : data};
        }
        res.json(response);
    });
});

但是,當我在郵遞員中運行該服務時,響應為錯誤“獲取數據時出錯”。 任何幫助我如何將這個值通過服務傳遞給$ near查詢作為參數? 謝謝

Express將所有傳入的值都視為字符串。 將maxDistance轉換為整數應該可以。

var max = parseInt(req.params.maxDistance);

暫無
暫無

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

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