我是 Node.js 新手,并且不断收到错误:Route.get() 需要回调函数,但收到 [object Undefined]
错误
我已经检查了以下问题,但要么不明白,要么我仍然做错了什么
Express routes: .get() requires callback functions but got a [object Object]
.get() requires callback functions but got a [object Undefined]
Error: Route.get() requires callback functions but got a [object Undefined]
Node Route.get() requires callback function but got a [object undefined]
我的文件结构是
server.js
routes/api/geolocations.js
routes/api/geolocations/regions.js
routes/api/geolocations/destination.js
根:server.js
var geolocation = require('./routes/api/geolocation')(app);
app.get('/geolocation/', geolocation.delegate);
然后我使用
将数据传递到routes/api/geolocations.js
geolocation.delegate(unparsedData);
从那里我解析数据并将其发送到适当的子路由。
<小时/>父级:geolocations.js 在我的routes/api/geolocations.js
var destination = require('./geolocations/destination');
var region = require('./geolocations/region');
module.exports = function(app) {
return {
app.get('./geolocation/region', region.delegate);
app.get('./geolocation/destination', destination.delegate);
delegate: function(unparsedData, req, res) {
var data =[setup package for child states using unparsedData]
//HERE Id like to pass the new `data` to region or destination using the following
region.delegate(data);
//OR
destination.delegate(data);
子项:routes/api/geolocations/regions.js
或routes/api/geolocations/destination.js< 中的region.js/destination.js
/
module.exports = function(app) {
return {
delegate: function(data, req, res) {
...do stuff
}
}
}
更新:我想我不知道在 server.js
中设置路线,或者是否可以在 geoloaction.js
中设置路线,这有关系吗,需要在 server.js 中做这样的事情吗?
var regions = require('./routes/api/geolocation/regions')([pass stuff here]);
geolocation.get('./routes/api/geolocation/regions', regions.delegate);
最佳答案
您应该使用express.js轻松设置和运行。
只需下载 IntelliJ IDEA,找到免费版本,然后安装即可。然后运行应用程序并转到"file"->“设置”->“插件”并搜索 NodeJS 然后安装。接下来您需要启用它。为此,请转到文件->设置->语言和框架->打开箭头-> JavaScript打开箭头->库->启用 Node.js Core。
文件结构
路线/api/geolocations.js
路线/api/geolocations/regions.js
路线/api/geolocations/destination.js
您可以查看下面的代码,这可能会帮助您入门。
//------------------------------------------------------------------------
var express = require('express');
var router = express.Router();
var regions = require('../api/geolocations/regions');
var destination = require('../api/geolocations/destination');
//------------------------------------------------------------------------
//------------------------------------------------------------------------
/* geolocation.js */
router.get('/', function(req, res, next) {
var region_ext = regions.to_export;
var destin_ext = destination.to_export;
res.render('index', {
title: 'Geolocation',
region: region_ext,
destination:destin_ext
});
});
module.exports = router;
//------------------------------------------------------------------------
//------------------------------------------------------------------------
/* region.js */
var to_export = function () {
return 'this is from regions';
}
module.exports.to_export = to_export();
//------------------------------------------------------------------------
//------------------------------------------------------------------------
/* destination.js */
var to_export = function () {
return 'this is from destination';
}
module.exports.to_export = to_export();
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//In app.js, just change
var routes = require('./routes/api/geolocations');
//------------------------------------------------------------------------
关于javascript - 在 Node.js 中将数据传递到子路由时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36637092/