iT邦幫忙

0

nodejs 文章評論 post 疑問

#model
  comments:[{
    body:String
  }]

#route

router.post('/comment',(req,res)=>{
  const postid=req.body.postid;
  const body=req.body.body;
  const comment ={
    "body":body
  }
Article.updateMany({ _id: postid }, { $push: { comments: comment }},function(err,test){
  if(err){
    console.log('err');
  }else{
    req.flash('success', 'Comment Added');
    res.redirect('/articles/house')
  }
});
});

#PUG
    form.comment-form(method='POST', action='/articles/comment')
     input(name='postid', type='hidden', value='#{house._id}')
     .form-group
      label Body
      textarea.form-control(type='text', name='body')
     br
     input.btn.btn-default(type='submit', name='submit', value='Add Comment')

請問一下 我想要讓評論跟文章存在一起
上網試滿多方法都沒成功
我現在這樣打他都有success 但資料都沒進去 請問是哪邊有問題或是 可以怎麼打呢

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
dragonH
iT邦超人 5 級 ‧ 2019-09-09 00:40:08

鍵盤解題

雖然我沒在用 mongoose

我都是用 monk

不過基本上

_id 應該都不會是純文字

而是 objectid

所以你 query 是

{ _id: postid }

更新沒反應應該就很正常了

然後你的寫法

是沒 error 就會跳成功

所以也很正常

參考

你同樣的 query

試試 find post 有沒有資料就知道了

看更多先前的回應...收起先前的回應...

請問我剛剛試了一下

  const _id = mongoose.Types.ObjectId(Article._id);

這樣宣告不過好像還是不行
我用get是有資料的

dragonH iT邦超人 5 級 ‧ 2019-09-09 09:10:31 檢舉

在 updatemany 中的 callback

把 test 的 三個值印出來

看分別是什麼

dragonH iT邦超人 5 級 ‧ 2019-09-09 10:21:37 檢舉

剛測了一下

好像不用轉成 objectid 也能正常使用/images/emoticon/emoticon17.gif

更新前

{
    "_id" : ObjectId("5d75aa41bd6a1ad77408cea6"),
    "title" : "A",
    "content" : "A content",
    "comments" : []
}

index.js

...
router.post('/edit-post', async (req, res) => {
  try {
    const { postId, comment } = req.body;
    const updateResult = await postsModel.updateMany({ _id: postId }, { $push: { comments: comment } });
    res.status(200).send({ updateResult }).end();
  } catch (err) {
    res.status(500).send({ msg: 'Server error' }).end();
  }
});
...

post data

{
	"postId": "5d75aa41bd6a1ad77408cea6",
	"comment": "first comment"
}

result

{
    "updateResult": {
        "n": 1,
        "nModified": 1,
        "ok": 1
    }
}

更新後

{
    "_id" : ObjectId("5d75aa41bd6a1ad77408cea6"),
    "title" : "A",
    "content" : "A content",
    "comments" : [ 
        "first comment"
    ]
}
{ n: 0,
  nModified: 0,
  opTime:
   { ts:
      Timestamp { _bsontype: 'Timestamp', low_: 45, high_: 1568022605 },
     t: 345 },
  electionId: 7fffffff0000000000000159,
  ok: 1,
  operationTime:
   Timestamp { _bsontype: 'Timestamp', low_: 45, high_: 1568022605 },
  '$clusterTime':
   { clusterTime:
      Timestamp { _bsontype: 'Timestamp', low_: 45, high_: 1568022605 },
     signature: { hash: [Binary], keyId: [Long] } } }

我照著你的run看看 不過出現一大堆看起來沒成功的訊息/images/emoticon/emoticon06.gif
你model裡的comments 是只有一個array
comments:[]
這樣嗎 在想是不是model建的時候就出問題了

dragonH iT邦超人 5 級 ‧ 2019-09-09 18:30:57 檢舉

你model裡的comments 是只有一個array
comments:[]

檔案不再手邊

不過我印象中是長

const postSchema = new mongoose.Schema({
    title: String,
    comments: Array,
});

這樣

你上面貼的訊息

nModified: 0

就是沒有資料更新

     input(name='postid', type='hidden', value=house._id)

我把pug裡 value='#{house._id}' 改成 value=house._id 就可以了耶
可以問問為什麼嗎 兩個語法的值不是都是這文章的ID嗎 /images/emoticon/emoticon19.gif

dragonH iT邦超人 5 級 ‧ 2019-09-10 09:17:20 檢舉

shrine90459

所以你沒有檢查到後端的 data 是否正確嗎/images/emoticon/emoticon21.gif

這篇應該完美解釋你的問題

Caution
Previous versions of Pug/Jade supported an interpolation syntax such as:

a(href="/#{url}") Link

This syntax is no longer supported. Alternatives are found below. (Check our migration guide for more information on other incompatibilities between Pug v2 and previous versions.)

我要發表回答

立即登入回答