Error in Passport JS: Trying to use an undefined function

I've been struggling with debugging my code in Express and Passport. I've tried following solutions from others but can't seem to get it right. Any help or useful links would be greatly appreciated.

Here is the error message along with the code and dependencies:

ERROR: passportjs TypeError: undefined is not a function ->> return done(null, user) is where the error occurs

Dependencies:

"body-parser": "^1.10.2",
"cookie-parser": "~1.3.3",
"express": "~4.11.1",
"express-session": "^1.10.3",
"hjs": "~0.0.6",
"mongodb": "^1.4.40",
"monk": "*",
"morgan": "^1.5.1",
"passport": "^0.2.1",
"passport-local": "^1.0.0",
"serve-favicon": "^2.2.0",

FILE NAME PASSPORT.JS

    passport.use('local-signup', new LocalStrategy({

            // by default, local strategy uses username and password, we will override with email
            usernameField: 'username',
            passwordField: 'password',
            passReqToCallback: true // allows us to pass back the entire request to the callback
        },
        function (req, res, email, password, done) {
            var db = req.db;
            var companyName = req.body.companyName;
            var fname = req.body.fname;
            var email = req.body.email;
            var username = req.body.username;

            if (fname === '' || companyName === '' || email === '' || username === '' || password === '' ) {
                console.log('Is this working????');
                res.render('business/register', {
                    error: 'You must fill in all fields.',
                    fname: fname,
                    companyName: companyName,
                    email: email,
                    username: username,
                    password: password
                });
            } else {
                var businesses = db.get('businesses');

                businesses.findOne({'email': email}, function (err, user) {

                    if (err) {
                        return done(err);
                    }

                    if (user) {
                        console.log('user exists');
                        console.log(user);
                        return done(null, false);
                    } else {

                        console.log('creating user');

                        password = auth.hashPassword(password);

                        businesses.insert({
                            email: email,
                            password: password,
                            companyName: companyName,
                            fname: fname,
                            username: username,
                            logo: '',
                            walkins: false
                        }, function (err, result) {
                            if (err) {
                                throw err;
                            }

                            var businessID = result._id.toString();

                            employees.insert({
                                business: ObjectId(businessID),
                                password: result.password,
                                fname: result.fname,
                                email: result.email,
                                smsNotify: true,
                                emailNotify: true,
                                admin: true
                            },function(err, user){
                                if (err) {
                                    throw err;
                                }
                                console.log(user);
                                return done(null, user);
                            });
                        });
                    }
                });
            }

        }
    ));`

Answer №1

The handler callback you provided includes an unnecessary res argument that is not necessary. The correct signature should be:

function(req, email, password, done)

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

What is the best way to invoke a controller method using jQuery within a cshtml file?

I am working on a project where I need to add user information to the database by calling a function in my controller class. The user's information is entered through a form created in a .cshtml file that interacts with an external JavaScript file. Is ...

Tips for aligning a dropdown button with the other elements in your navbar

I followed the code outline from a tutorial on We3schools, but I'm having an issue with the button not aligning correctly with the navbar. I attempted to adjust the code for proper alignment before publishing, but was unsuccessful. Here is the GitHub ...

Clear Input Field upon Selection in JQuery Autocomplete

I've been experimenting with the Azure Maps API to add autosuggestions for addresses in an input field. https://github.com/Azure-Samples/AzureMapsCodeSamples/blob/master/AzureMapsCodeSamples/REST%20Services/Fill%20Address%20Form%20with%20Autosuggest. ...

Is there a way to incorporate the load page plugin specifically for JavaScript while ensuring that my PHP code is still functional?

Is there a way to implement the loading page plugin "PACE" for PHP code execution instead of just JavaScript? I am more comfortable with PHP, CSS, and HTML, but not very experienced in JavaScript/AJAX. The PACE plugin is designed to show a progress bar fo ...

Create individual account pages with specific URLs in Next.js

I'm currently working on developing a website that will feature individual user pages showcasing their posts and additional information. I'm facing some difficulty in figuring out how to generate new links to access these user accounts. For insta ...

Loading google maps markers dynamically with ajax requests

I'm in the process of plotting around 120 markers on a Google Map by utilizing an ajax populated array containing google.maps.LatLng objects Here is my script <script type ="text/javascript"> $.ajaxSetup({ cache: false }); ...

Could not connect: AJAX Request denied?

I am currently hosting a Digital Ocean server that runs on a MEN stack composed of MongoDB, Express, and Node. The registration form can be accessed at: When attempting to choose an option from the dropdown menu, I encounter the following error message: G ...

What sets Fetch apart from ajax and XMLHttpRequest that makes it impressively faster?

Over the past few days, I have been working on optimizing a client table for a project. The table contains over 10k clients, and as a result, it was taking a long time to load. The front-end team had implemented pagination, filters, and reordering, which ...

Only one condition is met when using the Javascript if statement with the :checked and .bind methods

I need help creating an Override Button to disable auto-complete for a form using Javascript. I want the div "manualOverrideWarning" to display a warning if the button is selected, but my current function only works the first time the user presses the butt ...

What is the best way to adjust the height of a container to equal the viewport height minus a 300px height slider?

Forgive me for the rookie question, I know what I want to achieve should be simple but I seem to be having trouble articulating it in my search for solutions. Essentially, I am trying to set a section in HTML to the height of the viewport minus the height ...

The http-proxy-middleware feature automatically appends a forward slash ("/") in front of query parameters

I am looking to access an API through a proxy built with express. To achieve this, I am utilizing the http-proxy-middleware module. Below is the configuration I have set up: app.use( createProxyMiddleware('/api', { target: 'http://exam ...

Having trouble with my OpenAI API key not functioning properly within my React application

I've been struggling to implement a chatbot feature into my react app, specifically with generating an LLM-powered response. Despite going through documentation and tutorials, I haven't been successful in resolving the issue. My attempts involve ...

Angular Error: secure is not defined

Encountering the 'safe is undefined' error while interacting with HTML that has been dynamically inserted into a page via an AJAX call. For example, when selecting an option from a dropdown within this HTML, the error occurs and the dropdown rese ...

Issue 404: Trouble sending form data from React to Express

I'm facing an issue when trying to send form data from a React frontend to my Node.js/Express backend. The problem seems to be related to a 404 error. I've included only the relevant code snippets below for reference. Function for submitting the ...

Javascript - When I preview my file, it automatically deletes the input file

My form initially looked like this : <form action="assets/php/test.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> ...

Navigating through an array of latitude and longitude pairs during an AJAX request

Just starting out with PHP/AJAX and I could use some guidance. Currently, I have a leaflet map where I'm attempting to link two AJAX calls together. The initial AJAX call retrieves data based on a selected country ISO code. Subsequently, I've ut ...

Passing methods from child to parent components in Vue2 looped componentsHere is a

Currently, I am iterating through the root component, which contains a child component within it. <root-select v-for="offer in offers" > <child-options v-for="item in options" > </child-options> </root-select> However, when ...

Sorting Vue.js properties based on object keys

Currently, I am dealing with a complex object that contains multiple network interfaces. Each interface is represented by a key-value pair in the object: https://i.stack.imgur.com/klkhH.png My goal is to create a Vue.js computed property that filters thi ...

Errors encountered while starting Angular due to issues in package.json configuration

Summary: Encountered an error while using 'Angular' for the first time, indicating tsc was not found in the package.json file. Details: As a beginner with Angular, I followed an example from a book and attempted to start it with np ...

Error message: Node JS encountered a type error when attempting to access an undefined property

Exploring the world of node.js for the first time, I have ventured into building a small web application that showcases data from mongodb on the browser. Below is my index.js script: var express = require('express'); var app = express(); var b ...