2016-10-30 78 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) { ... }); 
}); 
相关问题