简体   繁体   中英

Bcrypt hash function returns undefined result

In a controller I'm trying to create a hash and save it with the user, using this section of code.

router.post('/', (req, res)=>{
   console.log(req.body.password, process.env.SALT_ROUNDS);
   bcrypt.hash(req.body.password, process.env.SALT_ROUNDS, function (err, hash) {
        if(err) {
           res.status(500);
        }  
        console.log("hash ", hash, req.body.password);
        // await req.context.models.User.create({
        //     username: req.body.username,
        //     email: req.body.email,
        //     password: hash })
        //       .then(function(data) {
        //           console.log("user saved")
        //           if (data) {
        //           res.send(data);
        //       }
        //  });
   });
});

But the result of the callback function inside hash argument is undefined. Here are the logs.

asd123 10

hash undefined asd123

Where is the problem, that hash is undefined ?

It turned out that process.env.SALT_ROUNDS was a string and I didn't quite understood that from the returned result. When I parse it to number +process.env.SALT_ROUNDS it worked.

asd123 10

hash $2b$10$Ty3JNeIBXUizrS85MsH8H.oTNsRtXzdiv9ZdamxnG7CmPD2CjSZG2 asd123

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM