【问题标题】:I am trying to save latitude and longitude to mongoose model but I am unable to store them我正在尝试将纬度和经度保存到猫鼬模型,但我无法存储它们
【发布时间】:2015-08-27 23:58:48
【问题描述】:

这是我的猫鼬模型

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

【问题讨论】:

  • 我搞定了。添加了一个位置数组。变量位置 = [];位置[0] = req.body.longitude;位置[1] = req.body.latitude;之后,我可以在邮递员中获得经度和纬度

标签: javascript node.js express mongoose postman


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    相关资源
    最近更新 更多