我正在嘗試創建一個架構,它有一個名為“business”的條目,它應該是陣列型別。
這是我嘗試過的。
export interface IBusinessShort{
savedOn: string;
}
export interface IUser {
name: string;
email: string;
username: string;
password: string;
phoneNumber: string;
avatar?: string;
business: Array<IBusinessShort>;
}
const schema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true },
username: { type: String, required: true },
password: { type: String, required: true },
phoneNumber: { type: String, required: true },
avatar: String,
business: { type: Array, default: []},
});
我在這里做錯了什么?
uj5u.com熱心網友回復:
根據您的架構,您正在嘗試將默認值設定為空陣列,但是,貓鼬已經定義了它。關于這一點,那部分代碼可以去掉。
陣列很特殊,因為它們隱式具有 [](空陣列)的默認值。
const ToyBox = mongoose.model('ToyBox', ToyBoxSchema);
console.log((new ToyBox()).toys); // []
參考:貓鼬 - 陣列
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371141.html
