Normoney123
Normoney123

Reputation: 59

controller function, next is not defined error

My code was working just fine, but out of no where I started getting this error. ReferenceError: next is not defined. No clue how I started getting it as I didn't change this function at all.

const Accounts = require('../data/accounts.model')

exports.addAccount = (req, res) => {
    const newAccount = new Accounts({
        Username: req.body.Username,
        Password: req.body.Password,
        AccountType: req.body.AccountType,
        Email: req.body.Email,
        Age: req.body.Age,
        Question1A: req.body.Question1A,
        Question2A: req.body.Question2A,
        Question3A: req.body.Question3A,
    });

    newAccount.save((err) => {
        if (err){
             return next(err);
  //error happens here^
        }
        res.send('Account created Successfully');
    });
};

Upvotes: 0

Views: 97

Answers (1)

next is the 3rd parameter to any middleware. So the code should be:

exports.addAccount = (req, res, next) => {

Upvotes: 1

Related Questions