带有mongoose的typescript:无法读取未定义的属性'CasterConstructor'

时间:2018-03-09 16:37:14

标签: typescript mongoose mongoose-schema mongoose-populate

我正在使用typescript和mongoose编写node.js应用程序。我使用的是打字稿模型方法,当我调用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'     在Array.create(/node_modules/mongoose/lib/types/documentarray.js:236:35)         。         。         。         。         

我没有看到我可能遗失的东西。有人可以回顾一下,让我知道我可能会错过什么,如果这是完全正确的方法。

更新:我切换到使用推送后,问题似乎出现在/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属性?

2 个答案:

答案 0 :(得分:0)

添加子文档时我不确定你在做什么? 您应该将其添加为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
});

答案 1 :(得分:0)

我找到了答案。我最终不得不将创建数据库连接的逻辑从单独的库移动到主项目中的inversify.config.js文件,并将mongoose连接注入每个存储库。这似乎解决了这个问题。