最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - express next throwing error as next is not defined - Stack Overflow

programmeradmin1浏览0评论

I am trying to pass some predefined functions in the callback of app.post() method. I am getting next is not defined error. Below is my code. Please suggest where I am doing wrong or am I missing any concept here?

var express = require('express');

var app = express()

app.post('/api/signup', function(req, res) {
validateParams(req, res, next),
dbCall(req, res, next),
sendResponse(req, res)
})

where I have each function defined and imported and returning next() after my process.

my validateParams function is below :

validateParams = function(req, res, next) {
    console.log("at validator ", req);
    next();
}

module.exports = validateParams;

my dbCall function is below :

dbCall = function(req, res, next) {
    console.log("at dbCall ", req);
    next();
}

module.exports = dbCall;

my sendResponse function is below :

sendResponse = function(req, res) {
    console.log("at dbCall ", res);
    res.send("Response sent successfully");
}

module.exports = sendResponse;

I am trying to pass some predefined functions in the callback of app.post() method. I am getting next is not defined error. Below is my code. Please suggest where I am doing wrong or am I missing any concept here?

var express = require('express');

var app = express()

app.post('/api/signup', function(req, res) {
validateParams(req, res, next),
dbCall(req, res, next),
sendResponse(req, res)
})

where I have each function defined and imported and returning next() after my process.

my validateParams function is below :

validateParams = function(req, res, next) {
    console.log("at validator ", req);
    next();
}

module.exports = validateParams;

my dbCall function is below :

dbCall = function(req, res, next) {
    console.log("at dbCall ", req);
    next();
}

module.exports = dbCall;

my sendResponse function is below :

sendResponse = function(req, res) {
    console.log("at dbCall ", res);
    res.send("Response sent successfully");
}

module.exports = sendResponse;
Share Improve this question edited May 31, 2023 at 21:16 Jonas 129k101 gold badges326 silver badges405 bronze badges asked Jun 4, 2018 at 7:41 Koushik Reddy UKoushik Reddy U 1881 gold badge2 silver badges15 bronze badges 2
  • can you share other functions (validateParams,dbCall) as well? – Rahul Sharma Commented Jun 4, 2018 at 7:43
  • 1 I think you want to work them as a middle-ware function, in this case, you can reference these functions right after the path in your app.post(). – Neeraj Wadhwa Commented Jun 4, 2018 at 7:44
Add a comment  | 

2 Answers 2

Reset to default 10

You probably forgot to add the next argument in your callback.

app.post('/api/signup', function(req, res, next) {
  validateParams(req, res, next),
  dbCall(req, res, next),
  sendResponse(req, res)
})

I think you are trying to use validateParams(req, res, next) and dbCall(req, res, next) as middleware functions. In this case, you need something like this:

const validateParams = (req, res, next) => {
  // do stuff here
  next();
}

const dbCall = (req, res, next) => {
  // do stuff here
  next();
}

app.post('/api/signup', validateParams, dbCall, function(req, res) {

sendResponse(req, res)
})

You can read more here

发布评论

评论列表(0)

  1. 暂无评论