簡體   English   中英

Node.js 中的“轉換錯誤”是什么? 我不知道出了什么問題

[英]What is 'cast error' about in Node.js? I can't figure out what's wrong

這是錯誤信息

{ [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 制作網絡應用程序,但我不知道這是怎么回事。

這是路由器代碼

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();

            } 

        })
    })


})

您可能正在使用 MongoDB 並且錯誤消息意味着它無法從您的請求body創建 ObjectID 。

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) { ... } );
} );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM