我为实践中的食物制作api我添加了
geometry: {
type: { default: 'Point' },
coordinates: [ Number ]
}
在foodtruck模型中的几何,我添加了
api.post('/add', (req, res) => {
let newFoodTruck = new FoodTruck()
newFoodTruck.name = req.body.name
newFoodTruck.foodtype = req.body.foodtype
newFoodTruck.avgcost = req.body.avgcost
newFoodTruck.geometry.coordinates = req.body.geometry.coordinates
newFoodTruck.save(err => {
if (err) {
res.send(err)
}
res.json({ message: req.body.name+' FoodTruck saved successfully' })
})
})
帖子中的几何体,用于在用户请求时获取用户的坐标数据 但是,当我尝试添加像(POST)http://localhost:3005/v1/foodtruck/add
这样的数据时{
"name": "Hot dog",
"foodtype": "Meet",
"evgcost": 5.99,
"geometry": {
"coordinates": [35, 34]
}
}
它返回
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>TypeError: Cannot read property 'coordinates' of undefined
<br> at C:/Users/Happy/Desktop/FoodTruckAPI/src/controller/foodtruck.js:15:59
<br> at Layer.handle [as handle_request] (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\layer.js:95:5)
<br> at next (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\route.js:137:13)
<br> at Route.dispatch (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\route.js:112:3)
<br> at Layer.handle [as handle_request] (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\layer.js:95:5)
<br> at C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:281:22
<br> at Function.process_params (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:335:12)
<br> at next (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:275:10)
<br> at Function.handle (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:174:3)
<br> at router (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:47:12)
<br> at Layer.handle [as handle_request] (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\layer.js:95:5)
<br> at trim_prefix (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:317:13)
<br> at C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:284:7
<br> at Function.process_params (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:335:12)
<br> at Immediate.next (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:275:10)
<br> at Immediate._onImmediate (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:635:15)
</pre>
</body>
</html>
我仍然混淆为什么它会返回未定义的坐标
答案 0 :(得分:0)
这是因为类型
geometry: {
type: { type: String, default: 'Point' },
coordinates: [ Number ]
},
我错过了类型:几何中的字符串 但是,我不知道为什么需要几何类型,因为我只需要坐标数