Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据放入Expressjs中mongodb的查询中_Javascript_Node.js_Express - Fatal编程技术网

Javascript 将数据放入Expressjs中mongodb的查询中

Javascript 将数据放入Expressjs中mongodb的查询中,javascript,node.js,express,Javascript,Node.js,Express,我在Expressjs中有这样一个查询: collection.find({location : {$near: [106.688227,10.774266], $maxDistance: 0.01}},function(e, docs) { res.json(docs); }); 它工作得很好,但当我从请求中获取参数并将其放入查询时,它就不起作用了: router.get('/locations', function(req, res) { var lat

我在Expressjs中有这样一个查询:

 collection.find({location : {$near: [106.688227,10.774266], $maxDistance: 0.01}},function(e, docs) {
        res.json(docs);
    });
它工作得很好,但当我从请求中获取参数并将其放入查询时,它就不起作用了:

router.get('/locations', function(req, res) {
    var lat = req.param('lat');
    var lng = req.param('lng');
    var radius = req.param('radius');
    var db = req.db;
    var collection = db.get('location');
    collection.find({location : {$near: [lng,lat], $maxDistance: radius}},function(e, docs) {
        res.json(docs);
    });

});
我如何在查询中替换lng、lat和radius


谢谢。

几条建议:1。尝试放置console.log语句,以查看lat/lng值确实是您期望的值。2.请尝试使用parseFloat函数,因为您从请求中获得的值可能是字符串,$near运算符可能需要数字。您好,Protostome,我在请求之前尝试了您的提示,但它不起作用,…。好的,请尝试使用以下结构:{location:{$near:{$geometry:{type:Point,座标:[lng,lat]},$maxDistance:radius}}}它也不起作用。。。。