2016-10-30 25 views
0

這是錯誤消息什麼是Node.js中的「cast error」?我想不出有什麼不對

{ [CastError: Cast to ObjectId failed for value "undefined" at path "_id"] 
    message: 'Cast to ObjectId failed for value "undefined" at path "_id"', 
    name: 'CastError', 
    kind: 'ObjectId', 
    value: 'undefined', 
    path: '_id', 
    reason: undefined } 

我試圖做出如何使用Node.js的web應用程序,我不知道這是什麼的。

這是路由器代碼

app.post("/fighter/:id/fight", function(req, res) { 

    Fight.create(req.body.fight, function(err, createdFight) { 
     if (err) 
      console.log(err) 
     else 
      res.redirect("/fighter/" + req.body.id); 
     Fighter.findById(req.params.id, function(err, foundFighter){ 

      if (err) 
       console.log(err) 
      else { 
       foundFighter.fights.push(createdFight); 
       foundFighter.save(); 

      } 

     }) 
    }) 


}) 

回答

0

您可能正在使用的MongoDB和錯誤消息意味着它不能從您的要求body創建一個對象ID。

app.post("/fighter/:id/fight", function(req, res) { 
    if (! req.body.fight) { return res.json({ error: "fight is empty" }) }; 

    Fight.create(req.body.fight, function(err, createdFight) { ... }); 
}); 
相關問題