我正在嘗試將一組物件保存到 mongoose,但這只會在資料庫中保存一個空陣列。以下是模型和架構
const HoldingSchema = new Schema({ symbol: String, quantity: Number });
const UserSchema = new Schema({
_id: {
type: String,
required: true,
},
holdings: {
type: [HoldingSchema],
default: undefined,
},
});
const UserModel = mongoose.model('Users', UserSchema, 'Users');
我嘗試使用以下代碼存盤物件陣列。但這會在持有的地方創建一個空陣列。
const testuser = new userModel({
_id: '001',
holding: [{ symbol: 'itc', quantity: 100 }],
});
await testuser.save(); // creates { _id: '001', holdings: [], __v: 0 }
是否無法存盤自定義物件陣列。如果不是,什么是替代方案?
uj5u.com熱心網友回復:
您的代碼中實際上有一個錯字,這就是它不保存您的資產的原因。
你已經寫了holding,而這個領域實際上是holdings
const testuser = new userModel({
_id: '001',
holdings: [{ symbol: 'itc', quantity: 100 }],
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/340236.html
上一篇:std::vector和移動語意
