S. Schenk
S. Schenk

Reputation: 2170

Populate does not retrieve the whole referenced object just the ids

I've been reading a few answers regarding this and yet I still can't get it to work.

My model objects aren't deeply nested and are quite simple. It's events that have a list of users attending them and users that have a list of events they've attended. like so:

let DinnerSchema = new mongoose.Schema({
  date: {
    type: Date,
    unique: true,
    timestamps: true,
    required: true
  },
  title:{type: String, require: true},
  attending: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }]
})

and the users:

let UserSchema = new mongoose.Schema({
  email: {
    type: String,
    lowercase: true,
    unique: true,
    required: true
  },
  name:{ type: String, require: true },
  password: {type: String ,required: true},
  dinners: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Dinner'
  }]
})

And for clarity here's the entire route that's using populate:

userpage.get('/', authCheck, (req, res) => {
  const options = { _id: '57ebbf48bd6914036f99acc7' }
  return Dinner
  .findOne(options)
  .populate('User', 'name') //I'VE TRIED ADDING 'name' BASED ON SOME ANSWERS I SAW 
  .exec((err, newDinner) => {
    if (err) {
      console.log(err)
      res.status(400).end()
    }
    console.log(newDinner) // SHOW'S USERS ID'S BUT NO OTHER FIELDS
    return res.json({
        sucsess: true,
        data: newDinner
      })
  })
})

If I understand correctly in the database itself there should only be a reference to the other model and not actually all of it's fields and the join happens with the populate. My db structure show's just the reference so that's ok.

I've tried specifying the name of the fields i'm after (the name field in this case) but that didn't work.

My population result always looks like the following and doesn't show any other fields except for the _id one:

{
 _id: 57ebbf48bd6914036f99acc7,
  date: 2016-09-27T22:00:00.000Z,
  title: '1',
  __v: 0,
  attending: [ 57ebbcf02c39997f9cf26891, 57ebbdee098d3c0163db9905 ] 
}

What am I screwing up here?

Upvotes: 1

Views: 1256

Answers (2)

Sachin
Sachin

Reputation: 2932

In mongoose populate receives 4 parameters.

  1. path
  2. selection(fields to be return) ,
  3. condition
  4. options (like {limit:10})

In your case you are not passing right path to populate. It should be

userpage.get('/', authCheck, (req, res) => {
  const options = { _id: '57ebbf48bd6914036f99acc7' }
  return Dinner
  .findOne(options)
  .populate('attending', 'name') 
  .exec((err, newDinner) => {
    if (err) {
      console.log(err)
      res.status(400).end()
    }
    console.log(newDinner) // SHOW'S USERS ID'S BUT NO OTHER FIELDS
    return res.json({
        sucsess: true,
        data: newDinner
      })
  })
})

Now it will return all the names of attending users.

Upvotes: 3

Markus
Markus

Reputation: 1563

you need to populate attending - that's your user reference in the dinner schema

Upvotes: 1

Related Questions