Guest User

mongoosejs

a guest
Jan 15th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var taskSchema = new Schema({
  2.     name: String,
  3.     completed: Boolean,
  4.     subtasks: [{name: String, completed: Boolean}],
  5.     description: String
  6. });
  7.  
  8. var listSchema = new Schema({
  9.     name: String,
  10.     author: String,
  11.     tasks: [
  12.     {
  13.         type: Schema.Types.ObjectId,
  14.         ref: 'Task'
  15.     }
  16.   ]
  17. });
  18.  
  19.  
  20. //this is how I'm trying to push a newly created task into the list
  21.  
  22.     List.findById(req.body.listId, function(err, list) {
  23.         var newTask = new Task({
  24.             name: req.body.newTaskName,
  25.             completed: false
  26.         })
  27.         newTask.save(function (err, task) {
  28.             if (err) {
  29.                 console.log(err);
  30.                 return res.sendStatus(500);
  31.             };
  32.             list.tasks.push(task._id);
  33.             return res.json({
  34.                 _id: task._id
  35.             });
  36.         });
  37.     });
  38.  
  39. //this is how I'm trying to populate tasks in the list
  40.  
  41.     List.find({
  42.         author: req.user.username
  43.     }).populate('tasks').exec(function (err, data) {
  44.         //some stuff    
  45.         return res.json(result);
  46.     });
  47.  
  48.  
  49.  
  50. nothing is being added to list.tasks, mongodb shell is showing an empty array
Advertisement
Add Comment
Please, Sign In to add comment