Rahul Kumar
Rahul Kumar

Reputation: 13

how to add newuser in mongoose data base in node js

I am following the course "learn nodejs by building 10 projects". I have coded the same way as my instructor, but when I am adding a new user to the mongo database its not adding everything except {"__v": 0}

var mongoose = require('mongoose');


mongoose.connect('mongodb://localhost/nodeauth');

var db = mongoose.connection;

// User Schema
var UserSchema = mongoose.Schema({
    username: {
        type: String,
        index: true
    },
    password: {
        type: String,
    },
    email: {
        type: String
    },
    name: {
        type: String
    },
    profileimage: {
        type: String
    }

});

var User = module.exports = mongoose.model('User', UserSchema);

module.exports.createUser = function(newUser,callback){
    newUser.save(callback);
}

//this is how im creating user

router.post('/register', function(req, res, next) {
  var name = req.body.name;
  var email = req.body.email;
  var username = req.body.username;
  var password = req.body.password;
  var password2 =  req.body.password2;

  var multer = require('multer');
  var upload = multer({ dest: './uploads' });

  // Check for Image Field
  if(req.files && req.files.profileimage){
      console.log('uploading File...');

      // File Info
      var profileImageOriginalName = req.files.profileimage.originalname;
      var profileImageName = req.files.profileimage.name;

      var profileImageMime = req.files.profileimage.mimetype;
      var profileImagePath = req.files.profileimage.path;
      var profileImageExt = req.files.profileimage.extension;
      var profileImageSize = req.files.profileimage.size;
  } else {
      // Set a Default Image
      var profileImageName = 'noimage.png';
  }

var newUser = new User({
          name: name,
          email: email,
          username: username,
          password: password,
          profileImage: profileImageName
      });

          // Create User
          User.createUser(newUser, function(err, user){
              if(err)throw err;
              console.log(user);
          });

          //Success Message
          req.flash('success', 'You are now registered and may log in');

          res.location('/');
          res.redirect('/');
  //}
});

please check the code and see where I'm going wrong

Upvotes: 0

Views: 2728

Answers (3)

Nedjm eddine
Nedjm eddine

Reputation: 1

just add newUser.save() after creating it

Upvotes: 0

Try this using your custom method createUser

let userModel= new User();
let data = {username:'alpha',password:'beta',name:'gamma'};

userModel.createUser(data,(err,doc) => {

if(err){
console.log(err);
}
console.log('saved');

})

Upvotes: 0

Tiago Neiva
Tiago Neiva

Reputation: 337

That result in mongoose means you are saving a user without values. You need to save an user for example like this:

let user= new User({

    userName: value,

    userPassword: value

  });

user.save();

or show a bit more how your trying to pass the values so we can check the best approach for it.

Upvotes: 2

Related Questions