繁体   English   中英

猫鼬填充返回空数组,但不填充返回数据

[英]Mongoose populate returns empty array but returns data without populate

我正在尝试获取约会,并用用户详细信息和医生详细信息填充它们。 我有这样的模式

let appointmentSchema = new Schema({
  reason: String,
  start: DateType,
  end: DateType,
  appointmentStamp: Number,
  userId: {
    type: ObjectId,
    ref: 'User'
  },
  doctorId: {
    type: ObjectId,
    ref: 'Doctor'
  },
  approved: Boolean,
  held: Boolean,
  timeOfAcceptance: Number,
  createdOn: { type: Number, default: (new Date().getTime()) },
  updatedAt: Mixed
}, {runSettersOnQuery: true})

而在跑步

Appointment.find({userId: authId}).populate('userId doctorId').exec()

我得到一个空数组,但是Appointment.find({userId:authId})。exec()返回该用户的所有约会,并提供了提供的userId,这是我错过的事情

这是样品预约

  {
    "_id": "5a0d424079191657fca9e853",
    "reason": "Say Hello",
    "approved": false,
    "held": false,
    "doctor": "5a0add9b577d7916187cd9f2",
    "patient": "59f5df20a3071554c5f843e1",
    "appointmentStamp": 1510927380000,
    "__v": 0,
    "createdOn": 1510817910134
  }

我尝试了一下,它奏效了,不知道为什么

patient: {
   type: String,
   ref: 'patient'
},
doctor: {
   type: String,
   ref: 'doctor'
}

我使用的是String类型,而不是ObjectId,查询就像

Appointment.find({patient: authId, canceled: false})
  .populate('patient doctor').exec()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM