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

时间:2015-06-12 04:14:15

标签: javascript node.js express mongoose postman

这是我的猫鼬模型

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

我在mongoose中将此请求保存在以下代码中:

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
    },

1 个答案:

答案 0 :(得分:0)

您正在使用的错误对象,loc类型是Array,而不是Object

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