在mongoose模式中使用ref填充集合失败

时间:2018-04-24 15:48:35

标签: javascript node.js mongodb express mongoose

我想将信用收集填充到用户集合中,这是我的信用模式

const CreditSchema = new Schema({
  userId: Schema.Types.ObjectId,
  credit: {
    type: Number,
    default: 0
  },
  log: [String]
})

我的用户架构就像这样

const UserSchema = new Schema({
  fullName: {
    type: String,
    trim: true,
    required: true
  },
  email: {
    type: String,
    unique: true,
    lowercase: true,
    trim: true,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  credit: {type: Schema.Types.ObjectId, ref: 'Credit' } //I suspect something is not right here
})

为什么我在下面的用户对象中看不到信用?

const user = await User.findOne({_id: req.query.id})
.populate('credit')
.exec()

console.log(user) // credit field is not present?

0 个答案:

没有答案
相关问题