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

Kev*_*oet 2 geospatial mongoose mongodb node.js

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

是因为我错误地将 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);
Run Code Online (Sandbox Code Playgroud)

功能:

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);
            });
    }
Run Code Online (Sandbox Code Playgroud)

POST 正文:

{
    "licenseplate": "xxaa22",
    "hit" : {
        "type" : "Point",
        "coordinates": [6.982654547382455, 46.88414220428685]
    }
}
Run Code Online (Sandbox Code Playgroud)

Shi*_*tri 5

Use parseFloat where you Insert the lattitude and longitude in mongoDB

var vehicle = new vehicle({
                    "licenseplate": licenseNumber,
                    "loc": { 
                        "type": "Point",
                        "coordinates": [parseFloat(lng), parseFloat(lat)]
                    }
                });
    vehicle.save();
Run Code Online (Sandbox Code Playgroud)