I'm using array of Strings to save emails:
var user = new Schema({
// other fields...
emails: [String]
});
Have troubles updating this field. Say, email1 and email2 are values I receive from the view:
This works well:
user.emails = [email1, email2];
user.save();
// fields are updated, all good
And this doesn't:
user.emails[0] = email1;
user.emails[1] = email2;
user.save(function(err, savedUser) {
console.log(savedUser.emails); // updated array [email1, email2]
// but if I retrieve now the user, the 'emails' field will not have above changes.
});
But, strangely, this works:
user.emails = [email1];
user.emails[1] = email2;
user.save();
// user.emails == [email1, email2];
Can anybody explain why this is happening?