0

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

2
  • 2
    How are you calling createUser ? Commented Aug 13, 2018 at 10:47
  • Please show your code that invokes createUser Commented Aug 13, 2018 at 11:20

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Rahul for what i see you have an error in the user schema, note that your saving the callback not the newUser , try to change it and see if it fixes. Last line where you export the createUser your doing this: newUser.save(callback); , try to do only newUser.save(); to see if it fixes
Rahul can you see if the data is going on submit ? check it on inspection with your browser to see if the user is going on post request. Also try to save directly instead of using the function your create. comment the code // Create User User.createUser(newUser, function(err, user){ if(err)throw err; console.log(user); }); and directly do newUser.save(); and do some console.log from the variables to see if they have any value aswell
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');

})

1 Comment

im already doing this but i seem to get empty values in mongoose db
0

just add newUser.save() after creating it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.