Error message: The compareSync function in Object.bcrypt is indicating that the passed string is undefined. Are there any

Within my rendered Nunjucks template, I have set it up to accept a username and password input from the user. Upon clicking the submit button, the goal is for the system to search through a database in my JavaScript Express file to verify if the entered credentials match any records.

However, I am encountering an error message that has been puzzling me.

I have carefully checked for spelling errors or other issues, but I am unsure of how to resolve this particular error. The specific error message reads:

Error: Illegal arguments: undefined, undefined at Object.bcrypt.compareSync

The code snippet in my Express file responsible for handling the submission process is as follows:

let usersdb = new DataStore({filename: __dirname + '/usersDB', autoload: true});
app.post('/user', express.urlencoded({extended:true}), function (req, res) {
    console.log(req.body);
    let username = req.body.username;
    let password = req.body.password;
    // Find user
    let auser = usersdb.find(function (user) {
        return user.username === username
    });
    if (!auser) {// Not found
        res.render("loginError.njk");
        return;
    }
//**** It seems like the error occurs on the following line *****
    let verified = bcrypt.compareSync(password, auser.passHash);
    if (verified) {

        let oldInfo = req.session.user;
        req.session.regenerate(function (err) {
            if (err) {
                console.log(err);
            }
            req.session.user = Object.assign(oldInfo, auser, {
                loggedin: true
            });
            res.render("welcome.njk", {user: auser});
        });
    } else {
        res.render("loginError.njk");
    }
});

My expectation is that when a user enters their username and password on the Nunjucks page, and if these match valid entries in the usersdb, the system should render the welcome page. Otherwise, it should display the login error page.

Your assistance with this matter would be greatly appreciated!

Answer №1

It seems evident that the issue stems from your verified variable. Make sure to carefully examine the data you are passing to bcrypt.compareSync. Additionally, double-check whether you have included select: false for the password property in your schema if you are utilizing mongoose/mongodb.

Answer №2

To fix the issue, make sure to utilize the findOne() function instead of find. This is because find will give you an array of objects like [{auserObject}], causing auser.passHash to be undefined. Using findOne will return just the object {auserObject}, allowing you to access auser.passHash appropriately.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to create a basic calculator in AngularJS with two textboxes specifically designed for calculating squares?

My code snippet is as follows: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <div ng-app="app"> <div ng-controller="Calcu ...

TypeScript async function that returns a Promise using jQuery

Currently, I am facing a challenge in building an MVC controller in TypeScript as I am struggling to make my async method return a deferred promise. Here is the signature of my function: static async GetMatches(input: string, loc?: LatLng):JQueryPromise& ...

Ways to incorporate lighting into the instancing shader

Is there a way to incorporate ambient and directional lighting into the shader when using InstancedBufferGeometry? For example, I am looking to add lighting effects to a specific instance, like in this example: Below is the vertex shader code: precision ...

Element not chosen in Angular version 6

Recently delving into Angular 6, I've been working on setting up form validation within an Angular form. Validation has been successfully implemented, but there's a minor issue with the select box displaying an empty first value. Here is my code ...

How can I effectively set up and utilize distinct npm modules on my local environment?

A React Native component I have created lives in a separate folder with its own package.json file, and now I want to use it in another project. The component named MyComponent is located in Workspace/MyComponent and has specific dependencies listed in its ...

Seeking assistance in incorporating items from one array into another array

I have a task involving two arrays - one named a, containing 5 values, and another named s which is currently empty. The objective is to identify the smallest value in array a, add it to array s, and then remove it from array a. Below is the code I have im ...

Convert h264 video to GIF using Node.js

Currently, I'm utilizing the "pi-camera" library to successfully record video in a raw h264 format on my Raspberry Pi. However, I am encountering an issue with the node.js library "gifify" which keeps throwing the error "RangeError: Maximum call stack ...

What is the best way to send variables from JavaScript to PHP while utilizing Ajax technology?

While I have noticed similar questions like this one before, I want to address my specific concerns. In previous examples, the questioner referred to using Ajax in a format similar to this: $.ajax({ type: "POST", url: 'logtime.php', ...

Establish the following 13 steps to configure the initial server state and retrieve the client state

Currently, I have 13 applications and I am utilizing Zustand as my state manager. Below is a simple layout example: <MainProvider> <div className="min-h-screen flex flex-col"> <Navbar></Navbar> <main className ...

What is the hexadecimal color code for a specific element's background?

How can I retrieve the background color code of a specified element? var backgroundColor = $(".element").css("background-color"); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="elemen ...

Using Three JS Circle Line Geometry to Color Negative Values

I've been experimenting with different methods to change the color of a circle based on positive and negative Z values. One approach I tried involved creating two separate line segments with different materials, but I encountered issues when the segme ...

When working with an outdated package, you may encounter a situation where babelHelpers is not

My current focus is on a vuetify v1.5 project. Unfortunately, one of the dependency packages (d3-flextree) is causing an issue with the 'babelHelpers is not defined' error. The solution I came across suggests using transform-runtime instead of th ...

Troubleshooting: Issue with Vue-Router failing to load component

I have been in the process of developing a website using Vue and Vue-Router locally. Upon completion, I push my changes with git to the production server which updates the site files. Recently, I encountered an issue with a payment status page that is wor ...

Angular directive ng-template serves as a component type

In the code snippet below, <!DOCTYPE html> <html> <head> ..... </head> <body ng-app="app"> <script type="text/ng-template" id="my-info-msg.html"> <s ...

Is it possible to adjust the height of the apprequests dialog?

I attempted the following code snippet: FB.UIServer.Methods["apprequests"].size = {width:600,height:320}; The width successfully adjusts, however, the height is restricted to a fixed value. Appreciate any help! ...

Exploring the integration of data from two Firestore collections using JavaScript

I manage two different types of collections, one being called CURRENCY-PAIR and the other Alerts. The collection CURRENCY-PAIR includes the following information: Currency-Pair Name Currency-AskPrice Currency-BidPrice On the other hand, the Alerts colle ...

How can we automate the process of assigning the hash(#) in Angular?

Is it possible to automatically assign a unique hash(#) to elements inside an ngFor loop? <div *ngFor="let item of itemsArray; index as i"> <h3 #[item][i]> {{ item }} </h3> </div> I would like the outp ...

The issue of receiving a 404 error when attempting to send a string via Ajax

When a key is pressed and released, a JavaScript function is triggered. The function is supposed to call a controller action and return a result, but instead, I am receiving a 404 error. I have set breakpoints at the beginning of the controller action, but ...

What is the best way to retrieve the most recent CMS posts information within a Gatsby-constructed project?

I created a static website using Gatsby and everything was working well. However, I encountered an issue when updating the titles and content of posts in Contentful CMS - the changes were not reflected when I refreshed the website. How can I ensure that ...

Display or conceal toggle in ruby utilizing jQuery

I'm facing an issue with the functionality of a button that is meant to hide and show a form using jQuery. Initially, I added a path to signup in the button so that it would display and function properly. Now, when I click the button, the form shows u ...