我已經為此尋找了幾個小時的答案——似乎有一些類似的問題沒有有效的答案——所以如果你能幫我解決它,那么你就可以擁有我的車、我的房子和我的第一個孩子——無論你想要什么!
當我為下面的模型創建記錄時,我需要一種向參與者嵌套陣列插入值的方法:
const recordSchema = new Schema (
{record:
[
{
eventDate: Date,
game: {
type: Schema.Types.ObjectId
,ref: "Game"
},
winner: {
type: Schema.Types.ObjectId
,ref: "Member"
},
particpants: [
{
type: Schema.Types.ObjectId
,ref: "Member"
}
]
},
{
timestamps: true,
}
],
}
)
創建記錄適用于除參與者之外的所有欄位,參與者仍然未填充:
Record.create({record: { game: game, winner: winner, participants: participants }})
我已經嘗試了幾種方法來嘗試訪問該嵌套陣列并(例如)在創建后更新,但到目前為止我還無法到達那里。請記住,這需要以編程方式創建 - 我不能硬編碼任何值。
非常非常感謝,如果您能幫助我解決這個問題。
Edit: adding the data being input as comment said this would be useful: console logging the variables right before the create returns the following: game: '6220ca4d8179d6685b4c05b7', winner: '6225ed9f21a3b31c86098b8d', participants: [ '6225ec458e34373cf7337d68', '6225ed9f21a3b31c86098b8d' ]
uj5u.com熱心網友回復:
您的架構中有錯字,正如您所看到的架構所期望的那樣,particpants但在您的架構中,.create()您正在傳遞participants.
您在架構中缺少iafter 。c
participants: [{ type: Schema.Types.ObjectId, ref: "Member" }]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/443300.html
標籤:node.js mongodb express mongoose
