【问题标题】:SailsJS geoNear IssuesSailsJS geoNear 问题
【发布时间】:2015-03-04 01:19:03
【问题描述】:

让 geoNear 在 SailsJS 中工作时遇到了很多麻烦。

在我的 UserController.js 中,我试图对我的用户模型中的 location 字段进行 geoNear 查询。它被定义为带有二维索引的 JSON。以下是字段数据的示例:

"location" : { "type" : "Point" , "coordinates" : [ 42.8003 , -73.8808 ]}

在我的 UserController 中,我的 geoSearch 方法如下所示:

 geoSearch: function(req, res) {
    //sails.log.verbose(req);
    var lat = req.param('lat');
    var lng = req.param('long');
    var limit = req.param('limit') || 50;
    var maxDistance = req.param('radius') || 5000; //meters
    sails.log.verbose('lat : ' + lat);
    sails.log.verbose('lng : ' + lng);
    sails.log.verbose('limit : ' + limit);
    sails.log.verbose('maxDistance : ' + maxDistance);
    if (!(lat && lng)) {
        //bad stuff
        res.badRequest('Missing lat or long!');
    } else {
        User.native(function(err, collection) {
          collection.geoNear(lng, lat, {
                    limit: limit,
                    maxDistance: maxDistance, // in meters
                    query: {}, // allows filtering
                    distanceMultiplier: 3959, // converts radians to miles (use 6371 for km)
                    spherical : true
          }, function(mongoErr, users) {
                  if (mongoErr) {
                      console.error(mongoErr);
                      res.send('_geoSearch failed with error='+mongoErr);
                  } else {
                      console.log('users=',users);
                      res.json(users.results);
                    }
             });
        });
    }
}

但是,当我提出以下请求时:

http://localhost:1337/api/user/location/?lat=42.8003&long=-73.8808

我得到了输出:

verbose: lat : 42.8003
verbose: lng : -73.8808
verbose: limit : 50    
verbose: maxDistance : 5000
{ [MongoError: exception: 'near' field must be point]
  name: 'MongoError',
  errmsg: 'exception: \'near\' field must be point',
  code: 17304,
  ok: 0 }

我已经为此苦苦挣扎了很长一段时间,任何帮助或指导将不胜感激。

其他信息:

MongoDB ~3.0 SailsJS ~0.10

https://github.com/mongodb/node-mongodb-native/blob/dd7bb687749ffab6ec4c4a6b052ef2cdffc0d780/lib/mongodb/collection.js#L1446

http://docs.mongodb.org/manual/reference/command/geoNear/

【问题讨论】:

  • 我应该知道geoNear 是什么吗?此外,“当前”不是一个版本。该错误具有相当的描述性:您正在向 mongo 发送 point 类型以外的其他内容
  • 我指定了当前版本#,数据是我单步调试时的一个点。我也尝试过旧坐标对。我还为您在 mongoDB 规范中引用了 geoNear。
  • 另外,我试过使用 .aggregate 和其他方法,总是报同样的错误。

标签: node.js geolocation sails.js sails-mongo


【解决方案1】:

我遇到了同样的问题,原因是:

但是,当我提出以下请求时:

http://localhost:1337/api/user/location/?lat=42.8003&long=-73.8808

因此,当服务器端接收到数据时,它会转换为“字符串”。 试试这个来解决它:

    var latitude = parseFloat(req.param('lat'));
    var longitude = parseFloat(req.param('long'));

希望这会有所帮助。

【讨论】:

  • 我在使用 NodeJs 时遇到了完全相同的问题。
【解决方案2】:

在我将它添加到我的 bootstrap.js 文件之前,我遇到了类似的问题

module.exports.bootstrap = function(cb) {
  // Ensure we have 2dsphere index on Property so GeoSpatial queries can work!
  sails.models.stores.native(function (err, collection) {
    collection.ensureIndex({ loc: '2dsphere' }, function () {

      // It's very important to trigger this callack method when you are finished
      // with the bootstrap!  (otherwise your server will never lift, since it's waiting on the bootstrap)
      cb();

    });
  });

};

基本上,您要确保您的收藏有一个与之关联的二维球体键。最后,在你的模型文件中不要忘记将包含地理坐标的键的类型定义为 json,如下所示:

module.exports = {

  attributes: {

    loc: {
      type: 'json'
    }

  }
};

希望它有所帮助。保重。

【讨论】:

  • 我试过了,我决定继续使用另一种技术,因为目前对 Sails 的支持似乎非常有限
  • 很遗憾,它对我有用,而且速度简直令人惊叹。从 100 个条目到 5000 个条目,没有明显的速度滞后。今晚我将对它进行 30000 个条目的压力测试。希望你有时会回来。我肯定不会回到其他任何事情上。仅供参考,这是一个现场生产现场,没有宠物项目。保重。
猜你喜欢
  • 1970-01-01
  • 2022-11-29
  • 2018-11-08
  • 2015-10-24
  • 2016-08-13
  • 1970-01-01
  • 2011-10-06
  • 2020-12-04
  • 1970-01-01
相关资源
最近更新 更多