【问题标题】:Meteor's mongodb insertion failed when storing GeoJSON Objects in field "loc"在字段“loc”中存储 GeoJSON 对象时,Meteor 的 mongodb 插入失败
【发布时间】:2016-09-27 04:04:30
【问题描述】:

我在我的 Meteor 应用程序中插入了一个对象 listingItemInjectionObj,它的属性之一如下

listingItemInjectionObj.loc = { 
    type: "Point", 
    coordinates: [ lat, lng ] // google api lat(), lng() stored in vars
};

但是,当我插入 Listings.insert(listingItemInjectionObj); 时,我收到以下错误 "insert failed: MongoError: insertDocument :: caused by :: 16804 location object expected, location array not in correct format" 我将基于 GeoJSON Objects 并在尝试 this SO Q 后来到这里。

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    您的数组中的纬度和经度顺序错误。 GeoJSON 在坐标中需要很长时间:

    listingItemInjectionObj.loc = { 
        type: "Point", 
        coordinates: [ lng, lat ]
    };
    

    【讨论】:

    • 谢谢你,我试过了,我试图挖掘错误代码 16804,但我什么也没有
    • 您是否检查过您的lng 和lat 是数字而不是字符串?
    • 是的,我也使用了 parseInt,但问题仍然存在。我不确定我使用的 MongoDB (2.6) 版本是否支持 GeoJSON?
    • 是的,确实如此。诡异的。我看不出你的代码有什么问题。你能展示一个实际的例子吗?您是否尝试过简单地从 mongo shell 中插入相同的对象?
    • 哦,您确定是 loc 属性导致了错误吗?对象中还有什么?
    【解决方案2】:
    Following code works:-
    
    Request payload:
    {"loc":["-12.62","21.48"]}
    
    Node.js backend:
    var lat, lon, loc = req.body.loc;
    if(loc && loc instanceof Array && loc.length) {
        lat = parseInt(loc[0], 10) || 0;
        lon = parseInt(loc[1], 10) || 0;
        data['loc'] = {
            type: "Point",
            coordinates : [lat, lon]
        };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2012-06-23
      • 2017-04-17
      • 2015-07-24
      相关资源
      最近更新 更多