在项目调试时收到此错误,错误是req.next不是函数

时间:2018-09-03 09:03:03

标签: javascript node.js express

我正在使用Express框架开发节点js。实际上,我正在将哈巴狗模板引擎切换到车把。那时我坚持这个错误。我在app.js文件中进行更改以更改模板引擎。请帮助我解决此问题。

/media/iron/1d6c195f-2350-423c-a3f0-0500c92e580a/admin-template/project/node_modules/express/lib/response.js:1003
    if (err) return req.next(err);
                        ^

TypeError: req.next is not a function
    at done (/media/iron/1d6c195f-2350-423c-a3f0-0500c92e580a/admin-template/project/node_modules/express/lib/response.js:1003:25)
    at Immediate.<anonymous> (/media/iron/1d6c195f-2350-423c-a3f0-0500c92e580a/admin-template/project/node_modules/express-handlebars/lib/utils.js:18:13)
    at runCallback (timers.js:763:18)
    at tryOnImmediate (timers.js:734:5)
    at processImmediate (timers.js:716:5)

节点:v9.11.0

Npm:6:4:1 App.js文件代码。帕格模板引擎代码已注释并使用车把代码。

var createError = require('http-errors');
var express = require('express');
var app = express();
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require("body-parser");
var exphbs = require("express-handlebars");
var session = require('express-session');
var firebase = require("firebase-admin");
var MongoStore = require('connect-mongo')(session);

//connect to MongoDB
mongoose.connect('mongodb://localhost/ForAuth',{ useNewUrlParser: true });
var db = mongoose.connection;

//handle mongo error
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
  //console.log('were connected!');
});

//use sessions for tracking logins
app.use(session({
  secret: 'work hard',
  resave: true,
  saveUninitialized: false,
  store: new MongoStore({
    mongooseConnection: db
  })
}));

// parse incoming requests
app.use(bodyParser.urlencoded({
    extended: true
}))
app.use(bodyParser.json());

// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'pug');

app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout:'layout'}));
app.set('view engine', 'handlebars');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
//var dashboardRouter = require('./routes/dashboard');

app.use('/', indexRouter);
app.use('/users', usersRouter);
//app.use('/dashboard', dashboardRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

0 个答案:

没有答案