我正在創建一個網站。我正在嘗試添加“警告”功能,但是當我將警告存盤到用戶中時,我得到了這個:0: 1
這就是我的意思0: 1
所以,這是我的代碼......導致這個問題的變數是newUser[0].warn = newWarn
const newMsg = {
title: msg[0].title,
body: msg[0].body,
author: msg[0].author,
postid: msg[0].postid
}
const warn = newUser[0].warn
const newWarn = warn.push(newMsg)
console.dir(newMsg)
newUser[0].warned = true
newUser[0].warn = newWarn
await newUser[0].save()
順便說一句,newMsg 設定正確:

這就是它所說的 0: 1


這應該存盤newMsg在陣列中,同時將其他內容保留在陣列中。
提前致謝 :)
編輯:
我發現了問題,但我不確定如何解決它。
它與 const newWarn = warn.push(newMsg)
uj5u.com熱心網友回復:
這是因為 warn.push 回傳長度為 1 的陣列
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/321263.html
標籤:javascript 节点.js MongoDB 猫鼬
上一篇:使用MongoDB和Mangoose使用Node.js查找并列印元素名稱,然后斷開與服務器的連接
下一篇:貓鼬模型保存后回傳承諾
