samedi 18 avril 2015

Populating an object with an array of objects that have it's _id

I have a bunch of tasks that when I create/edit I would like to be able to assign to a project. The way I have thought this should be done is the save the project ID to the task and the some how from the project collection reach into the task collection search for all objects with the matching project _id and then populate the tasks array with the correct tasks.


Now I don't know if this is the correct way and as you can see from my code I think I am half way there but I am not sure exactly how to populate a project with the correct tasks. I am using mongoose and it would be great if someone could let me know what I am doing wrong and how I could do it better.


I have a list of tasks that I want to link to projects.


TaskSchema:



var TaskSchema = new Schema({
title: { type: String},
description: String,
project: [{ type: Schema.Types.ObjectId, ref: 'Project'}]
});


Tast Controller:



'use strict';

var _ = require('lodash');
var Task = require('./task.model');

// Get list of tasks

exports.index = function (req, res) {
var populateUsers = {path:'users', select:'name profileImage email'};
Task
.find({})
.populate(populateUsers)
.populate('projects')
.exec(function (err, tasks) {
if (err) { //handle error
return handleError(res, err);
}
return res.json(200, tasks);
});
};

exports.show = function (req, res) {
var populateUsers = {path:'users', select:'name profileImage email'};
Task
.findById(req.params.id, function (err, task) { //get task
if (err) { //handle error
return handleError(res, err);
}
return task;
})
.populate(populateUsers)
.exec(function (err, task) { //use a callback to handle error or return tasks if everything is ok
if (err) { //handle error
return handleError(res, err);
}
return res.json(task); //return task filled with "users"
});
};


// Creates a new task in the DB.Î
exports.create = function(req, res) {
Task.create(req.body, function(err, task) {
if(err) { return handleError(res, err); }
return res.json(201, task);
});
};

// Updates an existing task in the DB.
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Task.findById(req.params.id, function (err, task) {
if (err) { return handleError(res, err); }
if(!task) { return res.send(404); }
var updated = _.merge(task, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, task);
});
});
};

// Deletes a task from the DB.
exports.destroy = function(req, res) {
Task.findById(req.params.id, function (err, task) {
if(err) { return handleError(res, err); }
if(!task) { return res.send(404); }
task.remove(function(err) {
if(err) { return handleError(res, err); }
return res.send(204);
});
});
};

function handleError(res, err) {
return res.send(500, err);
}


ProjectSchema:



var ProjectSchema = new Schema({
name: String,
dateCreated : {
type : Date,
default : Date.now
},
tasks: [{ type: Schema.Types.ObjectId, ref: 'Task'}]
});


Get Project List



// Get list of projects
exports.index = function (req, res) {
Project
.find({})
.populate('tasks')
.exec(function (err, projects) {
if (err) { //handle error
return handleError(res, err);
}
return res.json(200, projects);
});
};

Aucun commentaire:

Enregistrer un commentaire