I have looked out for this question, but unable to find the reason for this. I just started working on a new project though I'm able to get the results using one of the ways, but why it is not working when we directly export it.
When I try to export an async function like this, I get an error saying that hash
is not a function :
hashing.js
const bcrypt = require('bcrypt');
module.exports.hashing = async (password) => {
const salt = await bcrypt.genSalt(10);
const hashedResult = bcrypt.hashSync(password, salt);
console.log(hashedResult);
return hashedResult;
}
Importing hashing module to register module:
const dbConnection = require('../db/db');
const errorCodes = require('../db/error.code.json');
const hash = require('../controllers/shared/hashing');
module.exports.register = async (req, res) => {
try {
const db = await dbConnection();
const {email, username } = req.body;
const password = await hash(req.body.password); --> it says hash is not a function
const user = new db({email: email, password: password, username: username});
user.save((err, result) => {
if (err) {
res.send(errorCodes[err.code])
return;
}
res.status(201).json(result)
});
} catch (error) {
console.log(error);
}
}
But when I make these changes to the hashing.js, it works :
const bcrypt = require('bcrypt');
const hashing = async (password) => {
const salt = await bcrypt.genSalt(10);
const hashedResult = bcrypt.hashSync(password, salt);
console.log(hashedResult);
return hashedResult;
}
module.exports = hashing;
It says type error, I am attaching it to the question. Am I doing anything wrong? How can I get the function running?
Error Image: