【问题标题】:Saving coordinates from form从表单中保存坐标
【发布时间】:2013-06-11 08:52:29
【问题描述】:

我一直在尝试使用 node/express/backbone/mongoose 从表单中保存坐标 - 这可能与 MongoDB 本身有关

这是我的架构:

var MySchema = new mongoose.Schema({
 loc: {type:{type: String}, coordinates:[]}   
});

然后

var My = mongoose.model('My', MySchema);

var registerMy = function(loctype, longi, lati) {
var MyMy = new My({
  loc: {
    type:loctype,
    coordinates:[longi,lati]
        }
    });
 }

来自 HTML 表单

<form>
<fieldset>
  <label>
   Loctype:
  <input type="text" name="loctype" />
  </label>
 <label>
Longitude:
<input type="number" name="longi" />
</label>
<label>
 Latitude:
<input type="number" name="lati" />
</label>
</fieldset>
<p>
 <input type="submit" value="Register Now"/>
</p>
</form>

然后我得到这个错误:

{ [MongoError: 无法从对象中提取地理键,格式错误 几何?:{类型:“点”,坐标:[“37.677163”,“-1.696215”] }] name: 'MongoError', err: 'Can\'t extract geo keys from object, 畸形几何?:{类型:“点”,坐标:[“37.677163”, "-1.696215" ] }', 代码: 16572, n: 0, connectionId: 200, ok: 1 }

【问题讨论】:

  • 好的,刚刚解决(我真的花了一天多才发到这里,但后来它亮了)所以答案只是在坐标内添加数字(坐标:[数字]}并且效果很好

标签: mongodb mongoose


【解决方案1】:

我也遇到过同样的问题,你可能试图将位置数据保存为字符串数组,确保坐标(经度和纬度)是一个数字数组

即坐标必须是

[ 37.677163, -1.696215 ]

而不是

 [ "37.677163", "-1.696215" ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2021-07-08
    相关资源
    最近更新 更多