Populate doesn't return specified collection data

3 views
Skip to first unread message

Thian Kian Phin

unread,
Apr 25, 2018, 5:00:31 PM4/25/18
to mongodb-user

I have this credit schema

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

I hope it can be retrieve in below call

exports.getUser = async function(req, res) {

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

  res.json(user)
}

But what I get is userSchema's data, I don't see the data from credit schema. This is how my userSchema look like

const UserSchema = new Schema({
  name: {
    type: String
  },
  credit: {
    type: Schema.Types.ObjectId, 
    ref: 'Credit'
  }
})

I really scratch my head, I have the data in my db and the _id of userSchema is the same with userId of creditSchema.

Reply all
Reply to author
Forward
0 new messages