在Node.js中将数据传递到子路由时出现问题

Issue passing data to child routes in Node.js

本文关键字:路由 问题 js Node 数据      更新时间:2023-09-26

我是Node.js的新手,一直在获取Error: Route.get() requires callback functions but got a [object Undefined]错误

我检查了下面的问题,要么不理解,要么我还是做错了

Express routes:.get()需要回调函数,但得到了[object object]

.get()需要回调函数,但得到了一个[object Undefined]

错误:Route.get()需要回调函数,但得到了一个〔object Undefined〕

Node Route.get()需要回调函数,但得到了一个[对象未定义]

我的文件结构是

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

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


父母:我的routes/api/geolocations.js 中的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.jsroutes/api/geolocations/destination.js 中的儿童:region.js/testination.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,找到免费版本,然后安装即可。然后运行应用程序,转到文件->设置->插件,搜索NodeJS,然后安装。接下来你需要启用它。要做到这一点,转到文件->设置->语言&Frameworks->open-arrow->JavaScriptopen arrow->Libraries->Enable Node.js Core。

文件结构
routes/api/geolocations.js
routes/api/geolocations/regions.js
routes/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');
//------------------------------------------------------------------------

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