0

收到此错误:

Competition validation failed: results.0: Cast to ObjectId failed for value "{ name: 'David'}"

这是父母:

class Competition {
  @prop()
  compName: string
  

  @prop({ ref: () => CompetitionParticipant})
  results: Ref<CompetitionParticipant>[]
}

这是孩子:

class CompetitionParticipant  {

  @prop()
  name: string
}

这是它的调用方式:

const CompetitionResults = getModelForClass(Competition)
await new CompetitionResults({compName: 'competition name', results: [{name: 'David'}]}).save()
4

1 回答 1

0

这是因为您尝试将值保存{ name: 'David' }为不支持的参考值(请阅读此处了解更多信息),您需要提供且有效_id(在本例中为 an ObjectId),或mongoose.Document

最简单的解决方法是手动循环数组并单独保存它们(例如在批量插入调用中,或在数组上的 for 循环中)

于 2020-12-12T12:07:32.773 回答