mongodb getting TypeError: newUser.save is not a function

Published on Author Code Father

You have got some things wrong.

Something like this (User refers to your schema):

var user = new User();
user.name = req.body.name;
user.email = req.body.email;
user.password = req.body.password;
user.info = req.body.user_bio;
user.save().then(function(err, result) {
    console.log('User Created');});

should work better. Instead of passing a new object (which obviously doesn’t contain the save method), you are now creating a new object from the schema, setting the parameters, and then save it.

You then also have to change to this:

User.pre('save', function(next) {
    bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash(this.password, salt, function(err, hash) {
            this.password = hash;
            next();});});}
        
    

This is a hook, which is called every time before the user gets saved.

Comments

comments