2015-08-23 262 views
4

我是node.js的新手,所以這可能是一個愚蠢的錯誤。 我有一個MongoDB的模式是這樣的:mongoose - CastError:'Cast to undefined failed for value「[object Object]」at path「trainers」

seller_schema = mongoose.Schema({  
    name:String, 
    email:String, 
    trainers:[{ type: mongoose.Schema.Types.ObjectId, ref: 'trainers' }] 
}); 
seller_collection=db.model('seller',seller_schema); 

和trainer_collection是:

trainer_schema = mongoose.Schema({ 
     trainer_fname:String, 
     trainer_lname:String, 
     trainer_photo:String 
     }); 
trainer_collection=db.model('trainers',trainer_schema); 

我有一個功能,增加了賣方數據庫。情況有所不同,培訓人數也不相同。因此,我正在執行$push以在調用.save函數之前添加其他培訓師。

function createSeller(req, res){ 
var seller = new seller_collection({ 
     name: req.query.first_name, 
     email:req.query.email 

}); 
for(i=0;i<req.query.num_trainers;i++){ 
    try{ 
    var trainer_object={ "trainer_fname": req.query.trainer_fname, 
         "trainer_lname": req.query.trainer_lname, 
         "trainer_photo": req.query.trainer_photo 
         } 
    console.log(seller._id); 
     seller_collection.update({ _id: seller._id },{ 
       $push: { 
        "trainers":trainer_object 
       } 
     },function(error,course){ 
      console.log(error); 
      if(error){ 
       res.status(500).json({ 
        message: "failure", 
        status_code:"500", 
        error:error 
       }); 
      }else{ 
       res.status(200).json({ 
        message: "success", 
        status_code:"200" 
       }); 
      } 
     }); 

    }catch(ex){ 
     console.log(ex); 
     res.status(500).json({ 
        message: "failure", 
        status_code:"500", 
        res:res 
     }); 
    } 
} 
} 

堆棧跟蹤:

{ [CastError: Cast to undefined failed for value "[object Object]" at path "trainers"] 
     stack: 'Error\n at MongooseError.CastError (C:\\path\\node_modules\\mongoose\\lib\\error\\cast.js:18:16) at 
    SchemaArray.cast (C:\\path\\node_modules\\mongoose\\lib\\schema\\array.js:156:15) at SchemaArray.cast (C:\\path\\node_modules\\mongoose\\lib\\schema\\array.js:167:17) at Query._castUpdateVal (C:\\path\\node_modules\\mongoose\\lib\\query.js:2357:22)\n at Query._ 
    walkUpdatePath (C:\\path\\node_modules\\mongoose\\lib\\query.js:2273:27) at Query._castUpdate (C:\\path\\node_modules\\mongoose\\lib\\query.js:2202:23) 
    at Query.update (C:\\path\\node_modules\\mongoose\\lib\\query.js:2035:22)\n at Function.update (C:\\path\\node_modules\\mongoose\\lib\\model.js:174 
    1:13) at createSeller (C:\path\\routes\\seller.js:269:31) at Query.<anonymous> (C:\\path\\routes\\seller.js:12:21) at C:\\path\\node_modules\\mongoose\\node_modules\\kareem\\index.js:177:19 at C:\\path\\node_modules\\mongoose\\node_modules\\kareem\\index.js:109:16 at process._tickCallback (node.js:355:11)', 
     message: 'Cast to undefined failed for value "[object Object]" at path "trainers"', 
     name: 'CastError', 
     kind: undefined, 
     value: [{"trainer_photo":"x"}], 
     path: 'trainers' } 
+1

你*教員*物體看起來像一個字符串或JSON未解析。 –

+0

我不確定這是什麼意思。按照模式,培訓師的格式是正確的。 –

+1

你的*培訓者*對象不是一個對象,而是一個字符串。它不是* {} *而是*「[object Object]」*。 –

回答

2

對於那裏的人面臨着同樣的問題。

我改變了訓練師領域架構這樣trainers:[trainer_schema] 和編輯$push方法seller.trainers.push(trainer_object);