GeoJSON和Mongoose - Point必须只包含数字元素

时间:2018-03-27 16:08:39

标签: node.js mongodb mongoose geospatial

我收到以下错误:

MongoError: Can't extract geo keys: { _id: ObjectId('5aba6a88d366dbbf6c83a5d3'), gpshits: [ { coordinates: [ 6.982654547382455, 46.88414220428685 ], _id: ObjectId('5aba6a8fd366dbbf6c83a5d4'), type: "Point" } ], licenseplate: "xxaa22", createdAt: new Date(1522166408205), updatedAt: new Date(1522166415372), __v: 0 }  Point must only contain numeric elements

是不是因为我错误地将我的Point嵌套在模型中?我已经阅读了文档,但我找不到如何正确定位数组的示例。它没有给出任何错误。主要是试图在车辆牌照号码上记录GPS点击数据。

型号:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var VehicleSchema = new Schema({
        licenseplate: {type: String, required: true, unique:true},
        gpshits : [{
            type: { type: String },
            coordinates:[mongoose.Schema.Types.Mixed]
        }]
    }, 
    { 
        timestamps: true
    }
);

VehicleSchema.index({'gpshits' : '2dsphere'});

module.exports = mongoose.model('Vehicle', VehicleSchema);

功能:

function (req, res) {
        Joi.validate(req.body, Schemas.gpshit)
            .then(function () {
                return Vehicle.update({
                    licenseplate: req.body.licenseplate
                }, {
                    $push: {
                        'gpshits': req.body.hit
                    }
                }).exec();
            })
            .then(function () {
                return res.status(200).json({
                    success: true
                });
            })
            .catch(function (err) {
                console.log(err)
                return res.status(err.statusCode).json(err);
            });
    }

POST正文:

{
    "licenseplate": "xxaa22",
    "hit" : {
        "type" : "Point",
        "coordinates": [6.982654547382455, 46.88414220428685]
    }
}

2 个答案:

答案 0 :(得分:1)

在插入mongoDB中的经度和纬度的地方使用parseFloat

My API is(it is working fine)


 public class StockcountheaderController : ApiController
    {
        private adminv2Entities enqentities = new adminv2Entities();
       [HttpPost]
        private  IActionResult Stock([FromBody] List<spGetNewStockCountHeader_Result> 
 jsonvalues)    
{
  ObjectParameter TransactionId = new ObjectParameter("TransactionId", typeof(Int32));
  foreach (spGetNewStockCountHeader_Result Datastock in jsonvalues)
  {

    spGetNewStockCountHeader_Result Stockobject = new spGetNewStockCountHeader_Result();
    Stockobject.UserID = Datastock.UserID;
    Stockobject.created = Datastock.created;
    Stockobject.CompanyID = Datastock.CompanyID;
    Stockobject.modified = Datastock.modified;
    Stockobject.modifieduserid = Datastock.modifieduserid;
    Stockobject.confirm = Datastock.confirm;
    Stockobject.ShopId = Datastock.ShopId;
    enqentities.spGetNewStockCountHeader(Datastock.UserID, Datastock.created, Datastock.CompanyID, Datastock.modified, Datastock.modifieduserid, Datastock.confirm,Datastock.ShopId, TransactionId);
  }
return Ok(new { data = TransactionId.Value});    
}

答案 1 :(得分:-1)

我通过将数组对象分成它自己的模式并使用index()正确地将坐标字段设置为'2dsphere'来解决这个问题。