节点无法读取未定义的属性

时间:2018-06-04 13:13:33

标签: json node.js rest express

我为实践中的食物制作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 &#39;coordinates&#39; of undefined
            <br> &nbsp; &nbsp;at C:/Users/Happy/Desktop/FoodTruckAPI/src/controller/foodtruck.js:15:59
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at next (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\route.js:137:13)
            <br> &nbsp; &nbsp;at Route.dispatch (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\route.js:112:3)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:281:22
            <br> &nbsp; &nbsp;at Function.process_params (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at next (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at Function.handle (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:174:3)
            <br> &nbsp; &nbsp;at router (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:47:12)
            <br> &nbsp; &nbsp;at Layer.handle [as handle_request] (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\layer.js:95:5)
            <br> &nbsp; &nbsp;at trim_prefix (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:317:13)
            <br> &nbsp; &nbsp;at C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:284:7
            <br> &nbsp; &nbsp;at Function.process_params (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:335:12)
            <br> &nbsp; &nbsp;at Immediate.next (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:275:10)
            <br> &nbsp; &nbsp;at Immediate._onImmediate (C:\Users\Happy\Desktop\FoodTruckAPI\node_modules\express\lib\router\index.js:635:15)
        </pre>
    </body>
</html>

我仍然混淆为什么它会返回未定义的坐标

1 个答案:

答案 0 :(得分:0)

这是因为类型

  geometry: {
    type: { type: String, default: 'Point' },
    coordinates: [ Number ]
  },

我错过了类型:几何中的字符串 但是,我不知道为什么需要几何类型,因为我只需要坐标数