0

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:

enter image description here

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • 2
    Your export is called `hashing`, but you're importing it as though it were the default export (and calling it `hash` instead). Either change your export to `module.exports = async (password) => { /*...*/ };` or change your import to use the named export: `const {hashing} = require('../controllers/shared/hashing');` (or if you want to rename it `hash`, `const {hashing: hash} = require('../controllers/shared/hashing');`). But here in 2021, I suggest switching to ESM rather than CJS. – T.J. Crowder Apr 14 '21 at 11:47
  • 1
    @T.J.Crowder - Yes, thanks for the clarification. I will start using ESM for sure! Thank you! – Apoorva Chikara Apr 14 '21 at 11:54

1 Answers1

1

It didn't matter that your function is asynchronous.

When you export a function like this:

module.exports.hashing = () => {
    // ...
}

To use it properly, you have to do:

const {hashing} = require('../controllers/shared/hashing');
hashing();

// OR depending on your file context

const hash = require('../controllers/shared/hashing');
hash.hashing();

If you want to do:

const hash = require('../controllers/shared/hashing');
hash();

Export your function like this:

module.exports = () => {
    // ...
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
jpiazzal
  • 81
  • 6