带猫鼬的打字稿:无法读取未定义的 属性 'CasterConstructor'
typescript with mongoose: cannot read property 'CasterConstructor' of undefined
我正在使用打字稿和猫鼬编写 node.js 应用程序。我正在使用 typescript 模型方法,并在调用 create 方法创建新子文档时继续收到 "cannot read property CasterConstructor' of undefined"。下面概述了代码示例:
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
});
我找到了答案。我最终不得不将创建数据库连接的逻辑从单独的库移动到主项目中的 inversify.config.js 文件,并将猫鼬连接注入每个存储库。这似乎解决了问题。
我正在使用打字稿和猫鼬编写 node.js 应用程序。我正在使用 typescript 模型方法,并在调用 create 方法创建新子文档时继续收到 "cannot read property CasterConstructor' of undefined"。下面概述了代码示例:
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
});
我找到了答案。我最终不得不将创建数据库连接的逻辑从单独的库移动到主项目中的 inversify.config.js 文件,并将猫鼬连接注入每个存储库。这似乎解决了问题。