【问题标题】:Issue passing data to child routes in Node.js在 Node.js 中将数据传递给子路由的问题
【发布时间】:2016-04-15 02:06:31
【问题描述】:

我是 Node.js 的新手并不断收到 Error: Route.get() requires callback functions but got a [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

ROOT:server.js

var geolocation = require('./routes/api/geolocation')(app);
app.get('/geolocation/', geolocation.delegate);

然后我通过使用将我的数据传递给routes/api/geolocations.js

geolocation.delegate(unparsedData);

我从那里解析数据并将其发送到适当的子路由。


PARENT: 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);

儿童:region.js / destination.jsroutes/api/geolocations/regions.jsroutes/api/geolocations/destination.js

module.exports = function(app) {
    return {
        delegate: function(data, req, res) {

        ...do stuff
   }
 }
}

更新:我想我不知道在哪里设置我的路线,在server.js 或者如果我可以在geoloaction.js,有没有关系,需要在服务器上做这样的事情.js?

var regions = require('./routes/api/geolocation/regions')([pass stuff here]);
geolocation.get('./routes/api/geolocation/regions', regions.delegate);

【问题讨论】:

  • 我的第一个问题是你为什么要把这个结构弄得这么复杂。从一个更简单的结构开始,然后慢慢地将内容一次一个地分解为其他文件,这样您就可以准确地看到哪些是有效的,哪些是无效的。此外,geolocations.js 看起来像非法代码。您在对象定义的中间有 app.get() 调用。

标签: javascript node.js


【解决方案1】:

您应该使用 express.js 轻松设置和运行。

只需下载 IntelliJ IDEA,找到免费版本,然后安装。然后运行应用程序并转到 File->Setting->Plugin 并搜索 NodeJS 然后安装。接下来,您需要启用它。为此,请转到文件->设置->语言和框架->打开箭头-> JavaScript打开箭头->库->启用 Node.js 核心。

文件结构
路线/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');
//------------------------------------------------------------------------

【讨论】:

    【解决方案2】:

    jfriend00 是对的,你那里有点乱。也许您应该考虑使用 next(),因为如果您使用它,其他中间件将有机会操纵请求。

    【讨论】:

    • 好的,谢谢你的建议,我会考虑改变我的结构
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2016-09-21
    • 2017-08-24
    • 2019-02-25
    相关资源
    最近更新 更多