我正在尝试将纬度和经度保存到猫鼬模型,但我无法存储它们



这是我的猫鼬模型

var mongoose = require('mongoose'),
Schema = mongoose.Schema
var DriverSchema = new mongoose.Schema({
    carNumber : {type : String, required : true, unique : true},
    DriverName : {type : String, required : true},
    loc : {
        type: [Number],
        index:'2d'
    }
});

我正在尝试从邮递员那里发布纬度和经度,即:

carNumber         KA51B7499
DriverName        Sachin
longitude         74.9999
latitude          67.2563

我正在以下代码中将此请求保存在猫鼬中:

router.post('/',function(req,res){
    var Driver = new trackCab({
        carNumber: req.body.carNumber,
        DriverName: req.body.DriverName,
        loc: {
            type: [parseFloat(req.body.longitude),parseFloat(req.body.latitude)]
        }
    });
    //console.log(req.body.longitude);
    //console.log(req.body.latitude);
    Driver.save(function(err){
        if(err) console.log(err);
        console.log('User saved successfully!');
    });
});

但是,经度和纬度不会保存在 loc 对象中。

我在邮递员中返回的对象如下所示:

 {
        "_id": "55773165dda03a9c14cb8468",
        "carNumber": "KA51B7499",
        "DriverName": "Jagdeesh",
        "__v": 0
    },

您使用的错误对象,loc类型是数组,而不是对象

var Driver = new trackCab({
    carNumber: req.body.carNumber,
    DriverName: req.body.DriverName,
    loc:  [parseFloat(req.body.longitude),parseFloat(req.body.latitude)]
    })

相关内容

  • 没有找到相关文章

最新更新