gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:35:00 25 4
gpt4 key购买 nike

这是我的 Mongoose 模型

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

});

我正在尝试从 postman 处发布纬度和经度:

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 对象中。

我在 postman 中返回的对象如下所示:

 {
"_id": "55773165dda03a9c14cb8468",
"carNumber": "KA51B7499",
"DriverName": "Jagdeesh",
"__v": 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)]
})

关于javascript - 我正在尝试将纬度和经度保存到 Mongoose 模型,但我无法存储它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com