0

I'm using node.js and bcryptjs to register a new user and save their name/email/password to mongoDB in mlab.

Here is my code

const express = require("express");
const router = express.Router();
const gravatar = require("gravatar");
const bcrypt = require("bcryptjs");

// Load User model
const User = require("../../models/User");

// @route   GET api/users/test
// @desc    Tests users route
// @access  Public
router.get("/test", (req, res) => res.json({ msg: "Users Works" }));

// @route   GET api/users/register
// @desc    Register user
// @access  Public
router.post("/register", (req, res) => {
  User.findOne({ email: req.body.email }).then(user => {
    if (user) {
      return res.status(400).json({ email: "Email already exists" });
    } else {
      const avatar = gravatar.url(req.body.email, {
        s: "200", // Size
        r: "pg", // Rating
        d: "mm" // Default
      });

      const newUser = new User({
        name: req.body.name,
        email: req.body.email,
        avatar,
        password: req.body.password
      });

      bcrypt.genSalt(10, (err, salt) => {
        bcrypt.hash(newUser.password, salt, (err, hash) => {
          if (err) throw err;
          newUser.password = hash;
          newUser
            .save()
            .then(user => res.json(user))
            .catch(err => console.log(err));
        });
      });
    }
  });
});

// @route   GET api/users/login
// @desc    Login User / Returning JWT Token
// @access  Public

module.exports = router;

If I comment out line 37 "if(err) throw err;" I'm able to store the user credentials but the password will not store ( using postman )

The error I receive is ..

Error: Illegal arguments: undefined, string at _async (C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:214:46) at Object.bcrypt.hash (C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:220:13) at bcrypt.genSalt (C:\Users\Cody\Desktop\DevSoc\routes\api\users.js:36:16) at Immediate._onImmediate (C:\Users\Cody\Desktop\DevSoc\node_modules\bcryptjs\dist\bcrypt.js:153:21) at runCallback (timers.js:789:20) at tryOnImmediate (timers.js:751:5) at processImmediate [as _immediateCallback] (timers.js:722:5)

Where is this coming from? I can't see an error in my code.

Thanks

4
  • What does newUser.password contain? Actually log it on a request and find out. The error would indicate this is undefined. Commented Apr 24, 2018 at 22:45
  • You are correct, it is coming back undefined.. Now I'm trying to figure out why. Commented Apr 24, 2018 at 22:49
  • 1
    Start at req.body. Log it and then look back to the request being made if it's not even there. Check you have a body parser set up. If neither of those apply then it's more likely a problem with your model. Probably masking a field or similar. Commented Apr 24, 2018 at 22:50
  • The error was in my model.. Can't explain the frustration this has caused me. Thanks for the help mate Commented Apr 24, 2018 at 22:53

1 Answer 1

1

It appears that you are passing an undefined password to bcrypt.hash.

To make sure it doesn't get to that point, add earlier validation, probably at the first line of the POST route:

router.post("/register", (req, res) => {
  const {email, password} = req.body
  if (!email || !password) {
    return res.send('Must include email and password')
  }
  ...
})
Sign up to request clarification or add additional context in comments.

3 Comments

The error was actually in my model, but you are correct newUser.password was undefined
@4cody So why did you accept the answer then! Clearly your question does not include the part of the code showing the error ( probably a different name ) in the model, and clearly this does not give that as the reason. You really should have deleted the question or at least updated and self answered. In the current state, accepting an answer which is not really the solution is simply misleading to others.
The answer he gave is acceptable. But I get what you're saying, noted.

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.