如何創建具有默認值的物件陣列?
const schema = new mongoose.Schema({
login: {type: String, unique: true, required: true},
password: {type: String, required: true},
chatlist: [
{
channelname: {type: String, default: "frontend"},
channelid: {type: String, default: "619a6bfe5b0931f1e5dbaf2c"}
},
{
channelname: {type: String, default: "backend"},
channelid: {type: String, default: "619a71002954ba23a951bb8f"}
},
{
channelname: {type: String, default: "devops"},
channelid: {type: String, default: "619d69c190a85a40893b6522"}
},
]
})
當新用戶注冊時,上面的代碼不起作用我想在他的個人資料中添加默認聊天記錄
接下來是用戶必須能夠從資料庫中添加/洗掉聊天
我應該怎么做?聊天需要是物件還是檔案?
uj5u.com熱心網友回復:
const schema = new mongoose.Schema({
chatlist: {
type: Array,
defualt: []
}
})
所以為了在陣列中有一個結構,我個人會使用錯誤處理使請求正確。
欲了解更多資訊,請訪問此網站
uj5u.com熱心網友回復:
沒有找到任何好的解決方案,所以我只是這樣做了
const schema = new mongoose.Schema({
login: {type: String, unique: true, required: true},
password: {type: String, required: true},
chatlist: [{
channelname: String,
channelid: String
}]
})
const user = await MONGO_user.create({
login: login,
password: hashPassword,
})
user.chatlist.push(
{
channelname: "frontend",
channelid: "619a6bfe5b0931f1e5dbaf2c"
}, {
channelname: "backend",
channelid: "619a71002954ba23a951bb8f"
}, {
channelname: "devops",
channelid: "619d69c190a85a40893b6522"
})
await user.save()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371148.html
