【问题标题】:Can't extract geo keys (nodejs and mongo)无法提取地理键(nodejs 和 mongo)
【发布时间】:2015-05-01 17:29:52
【问题描述】:

我正在使用 nodejs (expres 框架) 和 mongodb 和 mongoose。我正在尝试将地理位置坐标保存到我的数据库中。

这是我的架构的样子:

//defining a schema
var TopicSchema = mongoose.Schema({
   topicTitle: String,
   topicDescription: String,
   topicDateCreated: { 
      type: Date, 
      default: Date.now 
   },
   fbId: String,
   twId: String,
   location: {
      type: {
        type : String
      },
   coordinates: [Number]
  }
});

TopicSchema.index({ location: '2dsphere' });
var Topic = restful.model('topic', TopicSchema);
Topic.methods(['get', 'put', 'post', 'delete']);
Topic.register(app, '/data');

我通过 socket.io 将数据传递给服务器。服务器收到数据,但保存后出现以下错误:

MongoError: Can't extract geo keys

【问题讨论】:

    标签: javascript node.js mongodb geolocation database


    【解决方案1】:

    错误在这里:

    var coordinates = LON + ", " + LAT;

    这里发生的情况是,您将两个数字与一个字符串组合在一起,您的 coordinates 就变成了一个字符串。 您的架构被定义为带有[Number] 的位置点数组。将您的 coordinates 作为字符串发送将违反您的架构定义。

    要解决您的错误,请创建一个地理点数组,例如var coordinates = [LON, LAT];

    【讨论】:

      【解决方案2】:

      Mongodb 使用经纬度系统来存储地理位置索引。这个坐标是浮点值,但你把它们放在一个数字上来使用。尝试以下方案:

      var TopicSchema = mongoose.Schema({
         topicTitle: String,
         topicDescription: String,
         topicDateCreated: { 
            type: Date, 
            default: Date.now 
         },
         fbId: String,
         twId: String,
         location: {
            type: {
              type : String
            },
         coordinates: [ ]
        }
      });
      

      您可以从这个repo 或这个answer 看到的其他示例

      【讨论】:

        【解决方案3】:
        $("#btnPostTopic").on("click", function(e)
                    {
                        //get the value of the inputfields and textarea
                        var title = $('#TopicTitle').val();
                        var description = $('#TopicDescription').val();
                        var LON = parseFloat($('#longitude').val());
                        var LAT = parseFloat($('#latitude').val());
        
                        var coordinates =  LON + ", " + LAT; 
                        console.log("COORDINATES are " + coordinates); 
        
                        var resultLocalStorageFB = document.getElementById("resultFB");
                        var resultLocalStorageTW = document.getElementById("resultTW");
                        resultLocalStorageFB.value = localStorage.getItem("useridfb");
                        resultLocalStorageTW.value = localStorage.getItem("useridtw");
                        console.log(resultLocalStorageFB);
                        console.log(resultLocalStorageTW);
        
                        var FbId = $('#resultFB').val();
                        var TWId = $('#resultTW').val();
                        console.log('Fb Id: ' + FbId);
                        console.log('TW Id: ' + TWId);
        
                        var Point = "Point";
        
                        result = { 
                            "topicTitle": title, 
                            "topicDescription": description, 
                            "fbId": FbId,
                            "twId": TWId,
                            "location": {
                                "type": Point,
                                coordinates 
                            }    
                        };
                        console.log("Values of inputfield & textarea: " + result);
        
                        //makes the connection to the server and send the data 
                        var socket = io.connect('http://localhost:3001');
                        socket.emit('sendTopic', result);   
                        //console.log("Gets send: " + result);
                    });
        
                });
        

        【讨论】:

          猜你喜欢
          • 2017-04-17
          • 2020-07-20
          • 2019-04-06
          • 2019-04-14
          • 2015-05-25
          • 1970-01-01
          • 1970-01-01
          • 2013-10-16
          • 2021-12-16
          相关资源
          最近更新 更多