node.js - Mongoose - can't save a document's properties -
so have users , teams. teams have array of users , add user team using adduser function. push teams user array , save results right after function go print out same team , there no users in array. not sure if .save() problem or what.
any appreciated, thank you.
... var userschema = new schema({ name: { type: string, required: true, unique: true }, age: number }); var teamschema = new schema({ name: { type: string, required: true, unique: true }, user: [{ type: schema.objectid, ref: 'user' }] }); userschema.statics.createuser = function(opts, cb) {...}; teamschema.statics.createteam = function(opts, cb) {...}; teamschema.statics.adduser = function(opts, cb) { team.find({_id: opts.team}).exec( function (err, team) { team[0].user.push(opts.user); team[0].save(function(err, save) { console.log("--------------------"); console.log(team[0]); //team contains added user here console.log("--------------------"); return cb(null); }); }); }; var user = mongoose.model('user', userschema); var team = mongoose.model('team', teamschema); var async = require('async'); var user1; var user2; var team; async.waterfall([ function(d){user.createuser({name :'test1'},function(err, user){ user1 = user; d(err); }); }, function(d){user.createuser({name :'test2',age :20},function(err, user){ user2 = user; d(err); }); }, function(d){team.createteam({name :'team'},function(err, obj){ team = obj; d(err); }); }, function(d){team.adduser({user : user1._id,team : team._id}, function(err){ console.log(team); d(err); }); }, function(d){team.adduser({user : user2._id,team : team._id}, function(err){ console.log(team); d(err); }); } ],function(err){ if(err){ console.log(err); } else{ user.count({},function(err,count){console.log("number of users:", count);}); team.count({},function(err,count){console.log("number of teams:", count);}); console.log(team); } }); returns: -------------------- { _id: 5583ed760958ab941a58bae9, name: 'team', __v: 1, user: [ 5583ed760958ab941a58bae7 ] } //user1 added -------------------- { __v: 0, name: 'team', _id: 5583ed760958ab941a58bae9, user: [] } //after function call team has no users -------------------- { _id: 5583ed760958ab941a58bae9, name: 'team', __v: 2, user: [ 5583ed760958ab941a58bae7, 5583ed760958ab941a58bae8 ] } //user2 added , user1 still there -------------------- { __v: 0, name: 'team', _id: 5583ed760958ab941a58bae9, user: [] } //no users again number of users: 2 number of teams: 1 //only 1 team exists
perhaps try using update , push simplify code?
findandmodify (mongodb command)
findoneandupdate (mongoose command)
team.findoneandupdate({"_id" : opts.team} , {$addtoset : { "user" : opts.user}}, function (err, foundupdatedteam) { return cb(err,foundupdatedteam); }, {new : true}); // sure update team variable reflect in database.
something this:
function(d){team.adduser({user : user1._id,team : team._id}, function(err, updatedteam) { team = updatedteam; console.log(team); d(err); });
few notes:
- i change team schema user teamusers or more descriptive..it's little confusing now.
- note use of addtoset, ensures if added user won't adding him twice.
- this code shorter , cleaner, don't need find object if can use dedicated function, if wish add user multiple teams i'd use update allows update multiple documents.
Comments
Post a Comment