我正在编写一个节点。使用typescript和mongoose的js应用程序。我正在使用typescript模型方法,并在调用create方法创建新子文档时继续收到“无法读取未定义的属性CasterConstructor”。代码示例概述如下:
seat.ts:
export let SeatSchema: Schema = new Schema({
travelerId: {type: Schema.Types.ObjectId, required: true},
seatClass: {type: Schema.Types.String, minlength: 5, maxlength:10, required: true, enum: [âFirstClassâ, âCoachâ]},
row: {type: Schema.Types.Number, required: true},
section: {type: Schema.Types.Number, required: true},
hasBoarded: {type: Schema.Types.Boolean, required: false}
});
plane.ts:
export let PlaneSchema: Schema = new Schema({
manufacturer: {type: Schema.Types.String, required: true},
seatsChart: [SeatSchema],
routeId: [{type: Schema.Types.ObjectId, ref: âroutesâ, required: true}];
iseat.ts:
export interface ISeat {
travelerId: string,
seatClass: string,
row: number,
section: number,
hasBoarded: boolean
}
iplane.ts:
export interface IPlane {
manufacturer: string,
seatsChart: Types.DocumentArray<ISeatModel>,
routeId: [string]
}
iseatmodel.ts:
export interface ISeatModel extends ISeat, Subdocument {
}
iplanemodel.ts:
export interface IPlaneModel extends IPlane, Document {
}
planerepository.ts:
constructor(db) {
this._model: Model<IPlaneModel> = db.model<IPlaneModel>(âPlaneâ, PlaneSchema);
}
.
.
.
addNewSeat(planeId: string, seat: ISeat) {
let plane: IPlaneModel = await this._model.findById(planeId);
let newSeat: ISeatModel = plane.SeatsChart.create(seatAttributes);
plane.seatsChart.push(newSeat);
let planeUpdated: IPlane = await plane.save();
}
这是我一直收到的错误:
TypeError:无法读取未定义的属性“casterConstructor”
at阵列。创建(/node\u modules/mongoose/lib/types/documentarray.js:236:35)
。
。
。
。
。
我看不到我会错过什么。请有人回顾一下,让我知道我可能遗漏了什么,以及这是否是正确的方法。
更新时间:
在我切换到使用push之后,问题似乎出现在/mongoose/lib/types/array中。js,检查手册人口:
function _checkManualPopulation(arr, docs) {
var ref = arr._schema.caster.options && arr._schema.caster.options.ref;
if (arr.length === 0 &&
docs.length > 0) {
if (_isAllSubdocs(docs, ref)) {
arr._parent.populated(arr._path, [], { model: docs[0].constructor });
}
}
}
由于某些原因,调用push时,\u schema对象未定义,从而导致异常。我不确定这是否是一个bug,或者是否没有调用其他东西来初始化\u schema属性?