node.js - Save/Update an array with Mongoose doesn't work -


i'm newbie mongoose... , i'm not able save/update array.

when create user login email, use this:

user.findone({ 'local.email' :  email }, function(err, user) {  var newuser = new user();  var name = req.body.name;  newuser.local.name    = name; newuser.local.array = [1, 2];  newuser.save(function(err) {  if (err)     throw err;     return done(null, newuser);  });  }); 

it works fine, user saved !

but when update user:

app.post('/change', function(req, res) {  var newuser = new user();  newuser.isnew = false;  newuser._id    = req.user._id; var newname = req.body.name;  newuser.local.array    = [3, 4];  newuser.save(function(err) {  if (err)     throw err;     return done(null, newuser);  });  }); 

it doesn't work, console log "versionerror: no matching document found". if comment line local.array invloved in update, works fine, user updated.

what's wrong code?

calling save on mongoose object make modification existing database entry if mongoose object obtained via find or findone. mongoose findoneandupdate can in 1 step.

or use mongo's standard method of updating existing documents. http://mongoosejs.com/docs/api.html#query_query-update


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -