【问题标题】:Mongoose, geospatial query for usersMongoose,用户的地理空间查询
【发布时间】:2016-06-08 21:54:33
【问题描述】:

我目前正在使用 nodeJS,使用 express 和 mongoDB 以及 mongoose 作为 ORM。当我创建一个用户并将它们保存到数据库时,我想查询它们的位置并保存它。这就是我目前正在做的事情,我有一个 UserSchema 和一个 location Schema。

我的 userSchema 只是将位置存储为字符串,并且在我的位置架构本身中

var locationSchema = new mongoose.Schema({
   name: String,
   loc: {
       type: [Number],
       index: '2d'
   }
});

mongoose.model('Location', LocationSchema);

然后在我的控制器中,我有以下内容

import json from '../helpers/json;
import mongoose from 'mongoose';
var User = mongoose.model('User);
module.exports = function() {
   var obj = {};

   obj.create = function (req, res) {
       var user = new User(req.body);
       user.roles = ['authenticated']
       user.location = getLocation(req);
       user.save(function (err) {
           if (err) {
              return json.bad(err, res);
           }

           json.good({
                 record: user,
           });
      });
    };

   return obj;

   function getLocation (req) {
       var limit = req.query.limit || 10;
       var maxDistance = req.query.distance || 1;
       maxDistance /= 6371;

       var coords = [];
       coords[0] = req.query.longitude;
       coords[1] = req.query.lattitude;

       Location.findOne({
           loc: {
               $near: coords,
               $maxDistance: maxDistance
           }
      }).limit(limit).exec(function (err, location) {
           if (err) {
              console.log(err);
           }

          return location.name;
     });
   }
};

我也尝试过使用 location.find 而不是 findOne 并返回 locations[0].name。

抛出的错误是说cast to the number failed for value undefined at loc。

我需要将位置数据从客户端发送到服务器吗?如果是这样,是否有最好的方法来实现这一点?我听说过 HTML5 Geolocation API,但从未使用过。

谢谢!

!!! -- 更新 --- !!

我已经开始在客户端使用 Geolocation API 在请求中将此数据发送到服务器。我像这样在客户端使用角度

(function() {
  'use strict';

  angular.module('opinionated.authentication')
  .controller('SignupController', SignupController);

  /* @ngInject */
  function SignupController ($state, appUsers, appToast) {
    var vm = this;
    vm.reset = reset;
    vm.create = create;
    vm.user = {
        name: '',
        username: '',
        email: '',
        password: ''
   };
   vm.location = {
      lattitude: '',
      longitude: ''
   };

   function create = (isValid) {
      if (isValid) {
        var user = new appUsers.single({
             name: vm.user.name,
             username: vm.user.username,
             email: vm.user.email,
             password: vm.user.password,
             lattitude: vm.location.lattitude,
             longitutde: vm.location.longitude
        });

        user.$save(function (response) {
           if (response.success) {
              appToast('Welcome to Opinionated, ' +     response.res.record.name);
              $state.go('authentication.wizard')
            } else {
              appToast(response.res.messsage);
            }
        });
      } else {
         appToast('Hmm... Something seems to be missing');
      }
    }
      function getPosition() {
          navigator.geolocation.getPosition(updatePosition);
      }

      function updatePosition (position) {
          vm.location.lattitude = position.coords.lattitude;
          vm.location.longitutde = position.coords.longitude;
      }

      getPosition();

     ....

我认为这与我现在获取坐标的方式有关。我的浏览器提示我允许使用我的位置,所以我至少在请求数据。但是,我更改了我的用户模式以保存经纬度,成功后这些值都没有保存。

【问题讨论】:

    标签: node.js mongodb express geolocation


    【解决方案1】:

    我发现了我的错误。我确实需要包含 Geolocation API 来获取用户坐标。然后我只是将坐标保存到数据库中,并从那里使用 mongo 的地理服务!现在一切正常。

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 2017-08-23
      • 1970-01-01
      • 2015-06-07
      • 2013-09-25
      • 2018-04-06
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      相关资源
      最近更新 更多