gpt4 book ai didi

带有 Mongoose 的 typescript : cannot read property 'CasterConstructor' of undefined

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:51 24 4
gpt4 key购买 nike

我正在使用 typescript 和 Mongoose 编写一个 node.js 应用程序。我正在使用 typescript 模型方法,并在调用创建方法创建新子文档时继续收到“无法读取未定义的属性 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();
}

这是我不断收到的错误:类型错误:无法读取未定义的属性“casterConstructor” 在 Array.create (/node_modules/mongoose/lib/types/documentarray.js:236:35) . . . . .

我没有看到我可能遗漏了什么。有人可以查看并让我知道我可能遗漏了什么,以及这是否是正确的方法。

更新:我改用push后,问题好像出在/mongoose/lib/types/array.js, _checkManualPopulation:

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 时未定义 _schema 对象,这会导致异常。我不确定这是一个错误,还是没有调用其他会初始化 _schema 属性的东西?

最佳答案

我不确定你在添加子文档时在做什么?您应该将其添加为 POJO。

以下代码已经过测试,可以正常工作。

async function addNewSeat(planeId: string, seat: ISeat) {
let plane = await PlaneModel.findById(planeId);
plane.seatChart.push(seat);
await plane.save();
}

然后像下面这样调用它:

let plane = await PlaneModel.create({flightNumber:"AR44"});
await addNewSeat(plane.id, {
travelerId:"x",
seatClass:"business",
row:4,
section:9,
hasBoarded:true
});
await addNewSeat(plane.id, {
travelerId:"y",
seatClass:"economy",
row:42,
section:1,
hasBoarded:true
});

关于带有 Mongoose 的 typescript : cannot read property 'CasterConstructor' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49198266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com