How to solve the abnormal match of bcrypt password?

  node.js, question
var bcrypt = require('bcryptjs');
 var SALT_WORK_FACTOR = 10;
 
 exports.testHash = function (req, res) {
 var password = '';
 bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
 if (err) return next(err);
 bcrypt.hash('123456', salt, function (err, hash) {
 if (err) return next(err);
 password = hash;
 })
 });
 bcrypt.compare('123456', password, function (err, isMatch) {
 if (err) return console.log(err);
 res.send(isMatch);
 })
 };

However, the return isfalse,

Because ..bcryptIt can’t be installed, so I used Taobao.bcryptjsInstead, it was used well before. I suddenly couldn’t match it these two days. Please comment!

This has nothing to do with whether Taobao is a mirror image, the key lies ingenSaltIt is an asynchronous method, it seems that you are relying on this asynchronous method to123456Hashing, become what you wantpasswordAnd then take thispasswordAnd123456UsecompareMethod comparison, it should be written like this:

var bcrypt = require('bcryptjs');
 var SALT_WORK_FACTOR = 10;
 
 exports.testHash = function (req, res) {
 var password = '';
 bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
 if (err) {
 return next(err);
 bracket
 bcrypt.hash('123456', salt, function(err, hash) {
 if (err) {
 return next(err);
 bracket
 password = hash;
 console.log(password);
 bcrypt.compare('123456', password, function(e, isMatch) {
 if (e) {
 return console.log(e);
 bracket
 res.send(isMatch);
 });
 })
 });
 };

If you really want to use synchronous writing, you should use the synchronous method provided by others:

var bcrypt = require('bcryptjs');
 var SALT_WORK_FACTOR = 10;
 
 exports.testHash = function (req, res) {
 var password = bcrypt.hashSync('123456', bcrypt.genSaltSync(SALT_WORK_FACTOR));
 
 res.send(bcrypt.compareSync('123456', password));
 };