Cannot read property 'reply' of null
Comment.findById(_comment.cid,function (err,comment) {
? ? ? ?var reply={
? ? ? ? ? ?from:_comment.from,
? ? ? ? ? ?to:_comment.tid,//點這個頭像的人要回復(fù)給他
? ? ? ? ? ?content:_comment.content
? ? ? ?}
? ? ? ?console.log(_comment.from);
? ? ? ?console.log(_comment.tid);
? ? ? ?console.log(_comment.content);
? ? ? ?console.log(comment+"77777777777");
? ? ? ?comment.reply.push(reply);
? ? ? ?comment.save(function (err,comment) {//
? ? ? ? ? ?if(err){
? ? ? ? ? ? ? ?console.log(err)
? ? ? ? ? ?}
? ? ? ? ? ?res.redirect("/movie/"+movieId)
? ? ? ?})
? ?})
Cannot read property 'reply' of null,一直顯示這個錯誤 console.log(comment+"77777777777");這個comment一直為null,那個大神請教一下
2018-06-08
當(dāng)前cid在數(shù)據(jù)庫中查詢的結(jié)果為null,null沒有方法,所以.reply報錯。
這段代碼我感覺有問題。findbyId應(yīng)該用comment表的頂層_id做參數(shù)查詢我覺得。
2018-01-23
if (!!_comment.cid) {
? ? ? ? Comment.findById(_comment.cid, function(err, comment) {
? ? ? ? ? ? var reply = {
? ? ? ? ? ? ? ? from: _comment.from,
? ? ? ? ? ? ? ? to: _comment.tid,
? ? ? ? ? ? ? ? content: _comment.content
? ? ? ? ? ? }
? ? ? ? ? ? // comment.reply.push(reply)
? ? ? ? ? ? // $addToSet: 不允許重復(fù); $push: 可以重復(fù)
? ? ? ? ? ? comment.update({$addToSet: {reply: reply}}, function(err, comment) {
? ? ? ? ? ? ? ? if (err) {
? ? ? ? ? ? ? ? ? ? console.log(err)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? console.log('@@@@' + comment)
? ? ? ? ? ? ? ? res.redirect('/movie/' + movieId)
? ? ? ? ? ? })
? ? ? ? })
? ? } else {
? ? ? ? var comment = new Comment(_comment)
? ? ? ? comment.save(function(err, comment) {
? ? ? ? ? ? if (err) {
? ? ? ? ? ? ? ? console.log(err)
? ? ? ? ? ? }
? ? ? ? ? ? res.redirect('/movie/' + movieId)
? ? ? ? })
? ? }
新版的MongoDB在需要往數(shù)組中添加數(shù)據(jù)時,使用push方法會報$pushAll錯誤。要使用$push或者是$addToSet來實現(xiàn),兩者的區(qū)別是$push可以重復(fù),而$addToSet不能重復(fù)。