Inserting an array in mongodb using mongoose in a schema where other fields are already added from user registration Inserting an array in mongodb using mongoose in a schema where other fields are already added from user registration mongoose mongoose

Inserting an array in mongodb using mongoose in a schema where other fields are already added from user registration


This is logical because first you do a it on various user objects (req.user / new user):

**var newUser = new User();

This will create a new User object, and then:

newUser.save(function (err, data) {

This will save the newly created user into a new document. If you want to use the .save, rewrite it to (reused your own code):

User.findOne({ 'username' :  username }, function(err, user) {      // In case of any error, return using the done method      if (err){          console.log('Error in SignUp: '+err);          return done(err);      }      user.courseEnrolled = req.body.courseEnrolled;      user.save(function (err, data) {    if(err)      throw err;    else {      console.log('course Updated');    }  });