0

I have designed the registration page in node.js but after clicking the submit button in the registration form I am getting the below error :

Some error occurred: Reference error : "bcrypt is not defined"

My user model is as follows:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({

    userName            : {type:String,default:'',required:true},
    firstName           : {type:String,default:''},
    lastName            : {type:String,default:''},
    email               : {type:String,default:''},
    mobileNumber        : {type:Number,default:''},
    password            : {type:String,default:''},
});

userSchema.pre('save', function(next) {
  var myuser = this;
  var SALT_FACTOR = 5;

  if (!myuser.isModified('password')) return next();

  bcrypt.genSalt(SALT_FACTOR, function(err, salt) {
    if (err) return next(err);

    bcrypt.hash(myuser.password, salt, null, function(err, hash) {
      if (err) return next(err);
      myuser.password = hash;
      next();
    });
  });
});

userSchema.methods.comparePassword = function(candidatePassword, cb) {
  bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
    if (err) return cb(err);
    cb(null, isMatch);
  });
};
module.exports = mongoose.model('myUser',userSchema);
  • 2
    Yes, it seems you didn’t define it anywhere. How would Node guess what you mean? – Sami Kuhmonen Nov 23 '17 at 05:46
  • Would you please have a look at my answer on **[Cannot find module 'bcrypt'](https://stackoverflow.com/questions/34546272/cannot-find-module-bcrypt/61558938#61558938)**? – Murat Yıldız May 02 '20 at 11:57

3 Answers3

3

You need to install and then require a bcrypt library, it isn't built into Node, thus why it is undefined.

There are several available, bcrypt.js is one that is written in native JS and still maintained. There is also the bcrypt package that binds to the C++ bcrypt implementation, which would run faster than pure JS implementations, but you might run into compilation errors; so you might want to stick to a JS implementation if you don't know how to troubleshoot that sort of thing.

To install bcrypt.js you would use npm: npm install bcryptjs

Then, require it at the top of your code along with mongoose:

var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');
Useless Code
  • 11,374
  • 4
  • 34
  • 39
  • 0 down vote accept yes I have installed bcryptjs and included it within my mongoose. But when I click on Signup button, it is generating a random password, not the password which I am entering while filling up the signup form. – Amitabha Ghosh Nov 28 '17 at 20:53
0

yes I have installed bcryptjs and included it within my mongoose. But when I click on Signup button, it is generating a random password, not the password which I am entering while filling up the signup form.

0

First install bcrypt using the following command in the npm

`npm install --save bcrypt`

Later import the bcrypt module in the backend node js service using:

const bcrypt = require('bcrypt');

Then using the bcrypt.hash() method we can encrypt the message or password.

Praveen G
  • 133
  • 1
  • 7