0

I see lot of questions about this but I can't find what is wrong. When I'm using populate to get the "Foreign Key" my field is undefined.

User Model :

var userSchema = new Schema({
    email        : { type: String, required: true, unique: true },
    password     : { type: String, required: true },
    firstname    : { type: String, required: true },
    lastname     : { type: String, required: true },
    created_at   : Date,
    updated_at   : Date,
    office       : [ { type: Schema.Types.ObjectId, ref: 'Office' } ]
});

var User = mongoose.model('User', userSchema, 'User');

module.exports = User;

Office Model :

var officeSchema = new Schema({
    name        : { type: String, required: true },
    address     : String,
    city        : String,
    geolocation : [ { type: Schema.Types.ObjectId, ref: 'Geolocation' } ],
    company     : [ { type: Schema.Types.ObjectId, ref: 'Company' } ]
});

var Office = mongoose.model('Office', officeSchema, 'Office');

module.exports = Office;

Populate code :

User.find({})
.populate('office')
//.populate('office', 'name') I tried this too
.exec(function (err, users) {
    if (err) return handleError(err);

    users.forEach(function(user){
        console.log('Office name: ', user.office.name);
    });
});

I want to get the user office name. But this user.office.name returns me undefined and when I do this user.office I can see the object with the name field. But I don't have the access to the name field.

John
  • 4,711
  • 9
  • 51
  • 101

2 Answers2

3

The office field in the userSchema is defined as array. So, in order to access its elements, use user.office[0].name, user.office[1].name, etc.

Otherwise, use a loop:

user.office
    .forEach(function(each) {
        console.log('Office name: ', each.name);
    });
Soubhik Mondal
  • 2,666
  • 1
  • 13
  • 19
  • Thanks. I have a question about populate. As you can see in my officeSchema I have company and geolocation object ID. It is possible to populate these two object on my user find at the same time when I populate the office ? – John Sep 20 '16 at 09:22
  • Yes you can. Check out either [Mongoose Deep Populate](https://github.com/buunguyen/mongoose-deep-populate) plugin or you can take a look at how Mongoose [populates across multiple levels](http://mongoosejs.com/docs/populate.html). – Soubhik Mondal Sep 20 '16 at 09:32
  • @Westen I'm not in the same case. I'm not in the same schema. User schema don't knows the company schema. So I think Mongoose Deep Populate plugin is the best solution for the moment – John Sep 20 '16 at 09:47
3

You just need to edit your query to

 populate({path: "office", populate: {path:"company"}})

It'll populate company data also.

Vaibhav Patil
  • 2,603
  • 4
  • 14
  • 22
  • Oh nice I can grab it with `user.office.company.name` it works thanks. But do you know if the mongoose populate performance is really good compared to SQL relationship ? – John Sep 26 '16 at 15:31
  • @John of course mongo is document database, the performance of mongo is better than SQL relationship.Kindly upvote if you like the answer. – Vaibhav Patil Sep 26 '16 at 15:54
  • Oh Okay. I heard that SQL is better to manage relationship between two table. So if NoSQL is good enough for that I will stay on mongo – John Sep 26 '16 at 17:16