2016-05-26 7 views
0

私はNodeJSとMongoDBでRest Servicesのリストを作成しています。これらのサービスの一つは、特定の位置

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

サービスがGETサービスであり、非常にうまく機能することによって、特定の範囲内のすべての要素をretriveするMongoDBのクエリの近くに$を実行します。 「

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

をしかし、私は郵便配達でサービスを実行すると応答がエラーである: 今私は、クエリの近くに$ maxDistanceの値を渡したいとサービスによって調整し、私だけmaxDistanceのために、このようにしてみていwoutldデータの取得中にエラーが発生しました。 どのように私はこの値をサービスとしてクエリの近くの$にパラメータとして渡すことができますか? ありがとう

答えて

1

Expressはすべての値を文字列として処理します。 maxDistanceを整数に変換するとうまくいくはずです。

var max = parseInt(req.params.maxDistance); 
関連する問題