Error: Route.get() requires a callback function but got a [object Undefined] despite module.exports = router; being there?
Error: Route.get() requires a callback function but got a [object Undefined] despite module.exports = router; being there?
我正在尝试结合我的 CRUD 和 TODO 应用程序。但是,即使我已将 "module.exports = router;" 添加到我的路由 js 文件、server.js 文件和控制器 js 文件中,我仍收到此错误。我将在下面添加这些文件和我的文件夹文件结构。我错过了什么吗?
folder/file structure
server.js 文件
require('./models/db');
require('./models/task');
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const bodyparser = require('body-parser');
const logger = require('morgan');
const mongoose = require('mongoose');
const favicon = require('serve-favicon');
const employeeController = require('./controllers/employeeController');
var app = express();
//setting up morgan middleware
app.use(logger('dev'));
app.use(bodyparser.urlencoded({
extended: true
}));
app.use(bodyparser.json());
app.set('views', path.join(__dirname, '/views/'));
app.engine('hbs', exphbs({ extname: 'hbs', defaultLayout: 'mainLayout', layoutsDir: __dirname + '/views/layouts/' }));
app.set('view engine', 'hbs');
//serving blank favicon to keep from throwing 404 errors
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
//setting up static path for serving static files
app.use(express.static(path.join(__dirname, 'public')));
//Bringing in the routes
const index = require('./routes/index');
const api = require('./routes/api');
app.use('/', index);
app.use('/api', api);
app.listen(3000, () => {
console.log('Express server started at port : 3000');
});
app.use('/employee', employeeController);
module.exports = router;
employeeController.js 文件
const express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const Employee = mongoose.model('Employee');
const Task = require('../models/task');
router.get('/', (req, res) => {
res.render("employee/addOrEdit", {
viewTitle: "Insert Module"
});
});
router.get('/test', (req, res) => {
res.render("employee/test");
});
router.get('/edit', (req, res) => {
res.render("edit");
});
router.get('/index', function(req, res) {
res.render('employee/index', {layout: 'main.hbs'});
});
router.get('/edit', function(req, res) {
res.render('employee/edit', {layout: 'main.hbs'});
});
router.post('/', (req, res) => {
if (req.body._id == '')
insertRecord(req, res);
else
updateRecord(req, res);
});
function insertRecord(req, res) {
var employee = new Employee();
employee.fullName = req.body.fullName;
employee.module = req.body.module;
employee.mobile = req.body.mobile;
employee.city = req.body.city;
employee.save((err, doc) => {
if (!err)
res.redirect('employee/list');
else {
if (err.name == 'ValidationError') {
handleValidationError(err, req.body);
res.render("employee/addOrEdit", {
viewTitle: "Insert Module",
employee: req.body
});
}
else
console.log('Error during record insertion : ' + err);
}
});
}
function updateRecord(req, res) {
Employee.findOneAndUpdate({ _id: req.body._id }, req.body, { new: true }, (err, doc) => {
if (!err) { res.redirect('employee/list'); }
else {
if (err.name == 'ValidationError') {
handleValidationError(err, req.body);
res.render("employee/addOrEdit", {
viewTitle: 'Update Module',
employee: req.body
});
}
else
console.log('Error during record update : ' + err);
}
});
}
router.get('/list', (req, res) => {
Employee.find((err, docs) => {
if (!err) {
res.render("employee/list", {
list: docs
});
}
else {
console.log('Error in retrieving module list :' + err);
}
});
});
function handleValidationError(err, body) {
for (field in err.errors) {
switch (err.errors[field].path) {
case 'fullName':
body['fullNameError'] = err.errors[field].message;
break;
case 'module':
body['moduleError'] = err.errors[field].message;
break;
default:
break;
}
}
}
router.get('/:id', (req, res) => {
Employee.findById(req.params.id, (err, doc) => {
if (!err) {
res.render("employee/addOrEdit", {
viewTitle: "Update Module",
employee: doc
});
}
});
});
router.get('/delete/:id', (req, res) => {
Employee.findByIdAndRemove(req.params.id, (err, doc) => {
if (!err) {
res.redirect('/employee/list');
}
else { console.log('Error in module delete :' + err); }
});
});
const sortTask = (a,b) => {
const taskA = a.task.toLowerCase();
const taskB = b.task.toLowerCase();
return (taskA < taskB) ? -1 : (taskA > taskB) ? 1 : 0;
}
module.exports = {
findAll: function (req,res){
Task
.find({})
.then(result => {
result.sort(sortTask)
res.render('employee/index', {layout: 'main.hbs'}, {tasks: result})
})
.catch(err => res.json(err))
},
create: function(req,res){
Task
.create(req.body)
.then(result => {
// result.sort(sortTask)
res.json(result)
})
.catch(err => res.json(err));
},
findOne: function (req,res){
Task
.findOne({_id: req.params.id})
.then(result => res.render('employee/edit', {layout: 'main.hbs'}, result))
.catch(err => res.json(err))
},
complete: function (req,res){
Task
.findOneAndUpdate({_id: req.params.id}, {completed: true})
.then(result => res.json(result))
.catch(err => res.json(err))
},
deleteOne: function (req,res){
Task
.remove({_id: req.params.id})
.then(result => res.json(result))
.catch(err => res.json(err))
},
updateName: function (req,res){
Task
.findOneAndUpdate({_id: req.body._id}, {task: req.body.task})
.then(result => res.json(result))
.catch(err => res.json(err))
}
}
module.exports = router;
api.js 文件
const router = require('express').Router();
const taskController = require('../controllers/employeeController');
router
.route('/task/:id')
.get(taskController.findOne)
.put(taskController.complete)
.delete(taskController.deleteOne)
router.post('/create', taskController.create);
router.post('/update', taskController.updateName);
module.exports = router;
index.js 文件
const router = require('express').Router();
const taskController = require('../controllers/employeeController');
router.get('/', taskController.findAll)
module.exports = router;
您在 employeeController.js 中多次使用 module.exports = {...}
而这不是它的工作原理,请参阅 docs 以了解更多信息
要解决这个问题,只需将路由器添加到第一个导出的对象中,如下所示:
module.exports = {
findAll: function (req,res) {
...
},
// rest of the functions
updateName: function(req,res) {
...
},
router
}
̶m̶o̶d̶u̶l̶e̶.̶e̶x̶p̶o̶r̶t̶s̶ ̶=̶ ̶r̶o̶u̶t̶e̶r̶;̶
然后在server.js中使用这样的路由器:
app.use('/employee', employeeController.router);
我正在尝试结合我的 CRUD 和 TODO 应用程序。但是,即使我已将 "module.exports = router;" 添加到我的路由 js 文件、server.js 文件和控制器 js 文件中,我仍收到此错误。我将在下面添加这些文件和我的文件夹文件结构。我错过了什么吗?
folder/file structure
server.js 文件
require('./models/db');
require('./models/task');
const express = require('express');
const path = require('path');
const exphbs = require('express-handlebars');
const bodyparser = require('body-parser');
const logger = require('morgan');
const mongoose = require('mongoose');
const favicon = require('serve-favicon');
const employeeController = require('./controllers/employeeController');
var app = express();
//setting up morgan middleware
app.use(logger('dev'));
app.use(bodyparser.urlencoded({
extended: true
}));
app.use(bodyparser.json());
app.set('views', path.join(__dirname, '/views/'));
app.engine('hbs', exphbs({ extname: 'hbs', defaultLayout: 'mainLayout', layoutsDir: __dirname + '/views/layouts/' }));
app.set('view engine', 'hbs');
//serving blank favicon to keep from throwing 404 errors
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
//setting up static path for serving static files
app.use(express.static(path.join(__dirname, 'public')));
//Bringing in the routes
const index = require('./routes/index');
const api = require('./routes/api');
app.use('/', index);
app.use('/api', api);
app.listen(3000, () => {
console.log('Express server started at port : 3000');
});
app.use('/employee', employeeController);
module.exports = router;
employeeController.js 文件
const express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
const Employee = mongoose.model('Employee');
const Task = require('../models/task');
router.get('/', (req, res) => {
res.render("employee/addOrEdit", {
viewTitle: "Insert Module"
});
});
router.get('/test', (req, res) => {
res.render("employee/test");
});
router.get('/edit', (req, res) => {
res.render("edit");
});
router.get('/index', function(req, res) {
res.render('employee/index', {layout: 'main.hbs'});
});
router.get('/edit', function(req, res) {
res.render('employee/edit', {layout: 'main.hbs'});
});
router.post('/', (req, res) => {
if (req.body._id == '')
insertRecord(req, res);
else
updateRecord(req, res);
});
function insertRecord(req, res) {
var employee = new Employee();
employee.fullName = req.body.fullName;
employee.module = req.body.module;
employee.mobile = req.body.mobile;
employee.city = req.body.city;
employee.save((err, doc) => {
if (!err)
res.redirect('employee/list');
else {
if (err.name == 'ValidationError') {
handleValidationError(err, req.body);
res.render("employee/addOrEdit", {
viewTitle: "Insert Module",
employee: req.body
});
}
else
console.log('Error during record insertion : ' + err);
}
});
}
function updateRecord(req, res) {
Employee.findOneAndUpdate({ _id: req.body._id }, req.body, { new: true }, (err, doc) => {
if (!err) { res.redirect('employee/list'); }
else {
if (err.name == 'ValidationError') {
handleValidationError(err, req.body);
res.render("employee/addOrEdit", {
viewTitle: 'Update Module',
employee: req.body
});
}
else
console.log('Error during record update : ' + err);
}
});
}
router.get('/list', (req, res) => {
Employee.find((err, docs) => {
if (!err) {
res.render("employee/list", {
list: docs
});
}
else {
console.log('Error in retrieving module list :' + err);
}
});
});
function handleValidationError(err, body) {
for (field in err.errors) {
switch (err.errors[field].path) {
case 'fullName':
body['fullNameError'] = err.errors[field].message;
break;
case 'module':
body['moduleError'] = err.errors[field].message;
break;
default:
break;
}
}
}
router.get('/:id', (req, res) => {
Employee.findById(req.params.id, (err, doc) => {
if (!err) {
res.render("employee/addOrEdit", {
viewTitle: "Update Module",
employee: doc
});
}
});
});
router.get('/delete/:id', (req, res) => {
Employee.findByIdAndRemove(req.params.id, (err, doc) => {
if (!err) {
res.redirect('/employee/list');
}
else { console.log('Error in module delete :' + err); }
});
});
const sortTask = (a,b) => {
const taskA = a.task.toLowerCase();
const taskB = b.task.toLowerCase();
return (taskA < taskB) ? -1 : (taskA > taskB) ? 1 : 0;
}
module.exports = {
findAll: function (req,res){
Task
.find({})
.then(result => {
result.sort(sortTask)
res.render('employee/index', {layout: 'main.hbs'}, {tasks: result})
})
.catch(err => res.json(err))
},
create: function(req,res){
Task
.create(req.body)
.then(result => {
// result.sort(sortTask)
res.json(result)
})
.catch(err => res.json(err));
},
findOne: function (req,res){
Task
.findOne({_id: req.params.id})
.then(result => res.render('employee/edit', {layout: 'main.hbs'}, result))
.catch(err => res.json(err))
},
complete: function (req,res){
Task
.findOneAndUpdate({_id: req.params.id}, {completed: true})
.then(result => res.json(result))
.catch(err => res.json(err))
},
deleteOne: function (req,res){
Task
.remove({_id: req.params.id})
.then(result => res.json(result))
.catch(err => res.json(err))
},
updateName: function (req,res){
Task
.findOneAndUpdate({_id: req.body._id}, {task: req.body.task})
.then(result => res.json(result))
.catch(err => res.json(err))
}
}
module.exports = router;
api.js 文件
const router = require('express').Router();
const taskController = require('../controllers/employeeController');
router
.route('/task/:id')
.get(taskController.findOne)
.put(taskController.complete)
.delete(taskController.deleteOne)
router.post('/create', taskController.create);
router.post('/update', taskController.updateName);
module.exports = router;
index.js 文件
const router = require('express').Router();
const taskController = require('../controllers/employeeController');
router.get('/', taskController.findAll)
module.exports = router;
您在 employeeController.js 中多次使用 module.exports = {...}
而这不是它的工作原理,请参阅 docs 以了解更多信息
要解决这个问题,只需将路由器添加到第一个导出的对象中,如下所示:
module.exports = {
findAll: function (req,res) {
...
},
// rest of the functions
updateName: function(req,res) {
...
},
router
}
̶m̶o̶d̶u̶l̶e̶.̶e̶x̶p̶o̶r̶t̶s̶ ̶=̶ ̶r̶o̶u̶t̶e̶r̶;̶
然后在server.js中使用这样的路由器:
app.use('/employee', employeeController.router);