Node.js 问题在哪里?错误:Route.get()需要回调函数,但得到了[object Undefined]
错误:Route.get()需要回调函数,但得到了一个[object Undefined] 我尝试在VS代码上运行应用程序,并收到此消息Node.js 问题在哪里?错误:Route.get()需要回调函数,但得到了[object Undefined],node.js,express,Node.js,Express,错误:Route.get()需要回调函数,但得到了一个[object Undefined] 我尝试在VS代码上运行应用程序,并收到此消息 /home/unkown/Node/node_modules/express/lib/router/route.js:202 throw new Error(msg); ^ Error: Route.get() requires a callback function but got a [object Undefined]
/home/unkown/Node/node_modules/express/lib/router/route.js:202
throw new Error(msg);
^
Error: Route.get() requires a callback function but got a [object Undefined]
at Route.<computed> [as get] (/home/unkown/Node/node_modules/express/lib/router/route.js:202:15)
Node/app\u server/controllers/locations.js
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const index = require('./app_server/routes/index');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'app_server', 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// 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;
/* GET 'home' page */
const homeList = (res, req)=>{
res.render('index', {title: 'Home'})
};
/* GET 'Location info' page */
const locationInfo = (res, req)=>{
res.render('index', {title: 'Location Info'})
};
/* GET 'Add review' page */
const addReview = (res, req)=>{
res.render('index', {title: 'Add Review'})
};
module.exports = {
homeList,
locationInfo,
addReview
};
const express = require('express');
const router = express.Router();
const ctrlLocations = require('../controllers/locations');
const ctrlOthers = require('../controllers/others');
/* Locations pages */
router.get('/', ctrlLocations.homelist);
router.get('/location', ctrlLocations.locationInfo);
router.get('/location/review/new', ctrlLocations.addReview);
/* Other pages */
router.get('/about', ctrlOthers.about);
module.exports = router;
Node/app\u server/router/index.js
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const index = require('./app_server/routes/index');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'app_server', 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// 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;
/* GET 'home' page */
const homeList = (res, req)=>{
res.render('index', {title: 'Home'})
};
/* GET 'Location info' page */
const locationInfo = (res, req)=>{
res.render('index', {title: 'Location Info'})
};
/* GET 'Add review' page */
const addReview = (res, req)=>{
res.render('index', {title: 'Add Review'})
};
module.exports = {
homeList,
locationInfo,
addReview
};
const express = require('express');
const router = express.Router();
const ctrlLocations = require('../controllers/locations');
const ctrlOthers = require('../controllers/others');
/* Locations pages */
router.get('/', ctrlLocations.homelist);
router.get('/location', ctrlLocations.locationInfo);
router.get('/location/review/new', ctrlLocations.addReview);
/* Other pages */
router.get('/about', ctrlOthers.about);
module.exports = router;
Router.get()
需要一个函数,但未定义
。从错误消息中可以清楚地看到这一点
在没有堆栈跟踪和从粘贴到此处的代码推断的情况下,问题似乎出现在以下位置之一:
/*位置页面*/
router.get('/',ctrlLocations.homelist);
router.get('/location',ctrlLocations.locationInfo);
router.get('/location/review/new',ctrlLocations.addReview);
/*其他页面*/
router.get('/about',ctrlOthers.about);
由于Ctrlocations
模块似乎可以很好地导出,因此问题必须出在ctrlOthers.about
方法中。要么未定义,要么未正确导出
希望这有帮助。Router.get()
需要一个函数,但未定义。从错误消息中可以清楚地看到这一点
在没有堆栈跟踪和从粘贴到此处的代码推断的情况下,问题似乎出现在以下位置之一:
/*位置页面*/
router.get('/',ctrlLocations.homelist);
router.get('/location',ctrlLocations.locationInfo);
router.get('/location/review/new',ctrlLocations.addReview);
/*其他页面*/
router.get('/about',ctrlOthers.about);
由于Ctrlocations
模块似乎可以很好地导出,因此问题必须出在ctrlOthers.about
方法中。要么未定义,要么未正确导出
希望这有帮助。什么是
应用程序中的用户
。使用('/users',users)代码>const homeList=(res,res)
将此更改为const homeList=(req,res)
所有路由都是如此。此错误发生在代码的哪一行?您必须回过头来查看堆栈跟踪,看看它在哪里碰到您的一个文件,可能是index.js
。这将告诉您哪个路线定义是错误的。既然您没有显示ctrlOthers
的代码,我猜router.get('/about',ctrlOthers.about)
可能是罪魁祸首,但也可能是您没有向我们展示的其他代码。@jfriend00我现在隐藏了此代码,但仍然是一样的。是的,我隐藏了一些资源,但不是必需的,如views/with.pug。@Yousaf如果这是另一个文件,则它是空的。app.use('/users',users)中的users
是什么代码>const homeList=(res,res)
将此更改为const homeList=(req,res)
所有路由都是如此。此错误发生在代码的哪一行?您必须回过头来查看堆栈跟踪,看看它在哪里碰到您的一个文件,可能是index.js
。这将告诉您哪个路线定义是错误的。既然您没有显示ctrlOthers
的代码,我猜router.get('/about',ctrlOthers.about)
可能是罪魁祸首,但也可能是您没有向我们展示的其他代码。@jfriend00我现在隐藏了此代码,但仍然是一样的。是的,我隐藏了一些资源,但不是必需的,如views/with.pug。@Yousaf如果这是另一个文件,它是空的。ctrlOthers.about是同一个文件ctrlLocations。这样我就没有把它列出来。还是一样的问题。谢谢兄弟。ctrlOthers
与导入声明中的文件不同。它是从controllers/others
文件导入的,此处未提及该文件的代码。如果该文件为空,则是问题所在。ctrlOthers.about是相同的文件CtrAllocations。这样我就没有把它列出来。还是一样的问题。谢谢兄弟。ctrlOthers
与导入声明中的文件不同。它是从controllers/others
文件导入的,此处未提及该文件的代码。如果该文件为空,则是问题所在。