NodeJS cannot import custom module although it exists

Viewed 77

Note: What you see below is the updated description of my problem, because I have been going down a rabbit hole and finding the root cause of a problem.

So, I found what's causing it (read 'OLD DESCRIPTION' below to know the context), but I have zero idea why is it being caused. So, the thing is, apparently Node cannot find the utils.getHash function (I have a separate file called utils.js which exports the getHash function), so it is never called, and execution never moves forward.

utils.js

...
const getHash = (password) => {
    return crypto.createHash('sha3-512').update(password).digest('hex')
}
...
module.exports = {
   getHash: getHash
}

Someone help please :(

OLD DESCRIPTION

There's a weird problem I am facing. I wrote a backend API server in ExpressJS, and one of the task it performs is user authentication. I am using MongoDB as the database, and Mongoose to connect and perform operations on it.

The problem I am facing is that the checkUserCreds function does not proceed after a point (commented in code), and Express just returns a blank JSON response.

And I say it it's weird, because I tested with the SAME code just 2 days back, it worked correctly like it should.

user.js

userSchema.statics.checkUserCreds = function (email, password) {
    return new Promise((resolve, reject) => {
        // Execution goes upto '$and' line, then it goes nowhere; no exceptions are raised
        User.findOne({
            $and: [{ email: email }, { password: utils.getHash(password) }]
        }, (err, userDoc) => {
            if (err) {
                reject({ status: "ERROR", message: err })
            } else if (userDoc) { // If valid credential
                console.log(`User with email '${email}' logged in`)
                resolve({ status: "OK", message: "Login successful!" })
            } else { // If invalid credential
                reject({ status: "ERROR", message: "Invalid credential!" })
            }
        })
    })
}

api.js

// Route - Login (POST: email, password)
router.post("/login", (req, res) => {
    // If user is already logged in, reject further login
    if (req.session.email) {
        res.json({ status: "ERROR", message: "Already logged in!" }).status(403).end()
    } else {
        // Get data from body
        var form = formidable()
        form.parse(req, (err, fields, files) => {
            if (err) {
                res.json({ status: "ERROR", message: err }).status(500).end()
            } else {
                // Check if credentials are valid
                User.checkUserCreds(fields.email, fields.password).then((result) => {
                    // This portion of code isn't reached either
                    req.session.email = fields.email
                    res.json(result).status(200).end()
                }).catch((err) => {
                    res.json(err).status(401).end()
                })
            }
        })
    }
})

Can anyone tell me why this is happening?

0 Answers
Related