0

I have the following schemas:

var SprintSchema = new Schema({
    name: {type: String},
    startDate: Date,
    endDate: Date,
    tasks: [{type: Schema.ObjectId, ref: 'Task'}]
});
var TaskSchema = new Schema({
    title: String,
    project: {type: Schema.ObjectId, ref: 'Project'},
    bp: Number,
    status: {type: Number, default: 0},
    description: String,
    assignee: {type: Schema.ObjectId, ref: 'User'},
    comments: [{
        author: {type: Schema.ObjectId, ref: 'User'},
        comment: String,
        systemMessage: String,
        time : { type : Date, default: Date.now}
    }],
    sDate: Date
});
var UserSchema = new Schema({
    googleId: {type: String, default: ''},
    firstName: {type: String, default: ''},
    lastName: {type: String, default: ''},
    email: {type: String, default: ''},
    profilePicture: {type: String, default: '/images/user.png'},
    accessToken: {type: String, default: ''},
    role: {type: String, default: 'employee'}
});

I now want to query the database for a specific sprint, with all tasks fully populated. By that I mean that the sprint object should not only contains the populated tasks, but the assignee (and project) for each task should be populated as well.

My code is now the following:

Sprint.findById(req.params.id).populate('tasks').populate('tasks.assignee').exec(function(err, sprint) {
        if(err) {
            console.log('SprintView:: error while fetching the sprint: ' + err);
            res.statusCode = 400;
            res.send('NOT OK');
            return;
        }
        for(var i = 0; i < sprint.tasks.length; i++) {
            console.log(sprint.tasks[i]);
        }
        console.log(sprint);
});

But the assignee is still not populated! What am I doing wrong?

1 Answer 1

1

Fixed this using extra populate commands:

Sprint.findById(req.params.id).populate('tasks').exec(function(err, sprint) {
    User.populate(sprint, {path: 'tasks.assignee'}, function(err, sprint) {
        Project.populate(sprint, {path: 'tasks.project'}, function(err, sprint) {
            //Do something cool here
        });
    });     
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.