错误:Route.get()需要回调函数

时间:2017-09-18 10:11:12

标签: node.js express

在我添加app-api文件并按照书中的说明后:Simon Holmes使用Mongo,Express,Angular和Node获取MEAN。 我重新启动应用程序,它显示错误: image

如何解决?

文件app.js:

var express = require('express');
var path = require('path');
var http = require('http');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
require('./app_api/models/db');
//require('./app_server/models/db');
var routes = require('./app_server/routes/index');
var routesApi = require('./app_api/routes/index');
var users = require('./app_server/routes/users');

var app = express();

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

// 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('/', routes);
app.use('/users', users);
app.use('/api', routesApi);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var 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;

app_api \ routes

中的文件index.js
var express = require('express');
var router = express.Router();
var ctrlLocations = require('../controllers/locations');
var ctrlReviews = require('../controllers/reviews');

// locations
router.get('/locations', ctrlLocations.locationsListByDistance);
router.post('/locations', ctrlLocations.locationsCreate);
router.get('/locations/:locationid', ctrlLocations.locationsReadOne);
router.put('/locations/:locationid', ctrlLocations.locationsUpdateOne);
router.delete('/locations/:locationid', ctrlLocations.locationsDeleteOne);

// reviews
router.post('/locations/:locationid/reviews', ctrlReviews.reviewsCreate);
router.get('/locations/:locationid/reviews/:reviewid', 
ctrlReviews.reviewsReadOne);
router.put('/locations/:locationid/reviews/:reviewid', 
ctrlReviews.reviewsUpdateOne);
router.delete('/locations/:locationid/reviews/:reviewid', 
ctrlReviews.reviewsDeleteOne);

module.exports = router;

app_api \ controllers中的文件位置:

 var mongoose = require('mongoose');
 var Loc = mongoose.model('Location');
 var sendJsonResponse = function(res, status, content) {
     res.status(status);
     res.json(content);
 };
 module.exports.locationsCreate = function (req, res) {
 sendJsonResponse(res, 200, {"status" : "success"});
 };

app_api \ controllers中的文件reviews.js

var mongoose = require('mongoose');
var Loc = mongoose.model('Location');
var sendJsonResponse = function(res, status, content) {
    res.status(status);
    res.json(content);
};
module.exports.reviewsCreate = function (req, res) {
sendJsonResponse(res, 200, {"status" : "success"});
};

1 个答案:

答案 0 :(得分:0)

堆栈跟踪告诉您问题的确切位置:

ap_api\routes\index.js:7

即。 index.js的第7行,即:

router.get('/locations', ctrlLocations.locationsListByDistance);

错误表明回调未定义。这里的回调'是ctrlLocations.locationsListByDistance,它似乎不在您的位置控制器中。