【问题标题】:Node.js server / express module not sending data to clientNode.js服务器/快递模块不向客户端发送数据
【发布时间】:2020-08-26 20:52:27
【问题描述】:

更新

根据@AndrewNolan 的建议,我将代码更改为使用async waterfallasync series 的承诺链:

https://caolan.github.io/async/v3/docs.html#waterfall

https://caolan.github.io/async/v3/docs.html#series

app.use 被识别没有问题,但没有发送给客户端。

router.get 中的 console.log 不记录任何内容,但 outisde 记录。

// collect data from the database associated with user on login
app.post('/login', function(request, response, callback) {

  // outline the parameters for the login
  var parameters = [ request.body.input.sessionAppID, request.body.input.sessionUserID ];

  // outline an async waterfall to collect the login details
  async.waterfall([
    function(callback) {

      // create a new mongo client for collecting the details of the user
      var client = new MongoClient(url, mongoOptions);

      // call the async function to collect the user details
      loginUser(parameters, client).then(function(user) {

        // close the client after the user details have been collected
        client.close().then(function() {
          // set the callback for the first waterfall as the user
          callback(null, user);
        })
      });
    },
    function(user, callback) {

      // set the application id of the login
      var application = user.appID;

      // set an async series of functions to collect the login information
      async.series({
        readings: function(callback) {

          // create a new mongo client for collecting the users devices
          var client = new MongoClient(url, mongoOptions);

          // call the async function to collect the devices of the user
          readings(user, client).then(function(readings) {

            // close the client after the readings have been collected
            client.close().then(function() {

              // set the callback for the first waterfall as the user
              callback(null, readings);
            })
          })
        },
        alerts: function(callback) {
          // create a new mongo client for collecting the users devices
          var client = new MongoClient(url, mongoOptions);

          // call async function to collect login alerts
          loginAlerts(application, client).then(function(alerts) {

            // close the client after the alerts have been collected
            client.close().then(function() {

              // set the callback for the alerts
              callback(null, alerts);
            })
          })
        },
        polygons: function(callback) {
          // create a new mongo client for collecting the users devices
          var client = new MongoClient(url, mongoOptions);

          // call async function to collect the polygons on loginUser
          loginPolygons(application, client).then(function(polygons) {

            // close the client after the polygons have ben collected
            client.close().then(function() {

              // callback when the login polygons are collected
              callback(null, polygons)
            })
          })
        }
      },
      function(err,results) {

        // create a router to send data to the client / webpage
        var router = new express.Router();

        // send login data to the webpage as json
        router.get('/loginData', cors(), function(req, res) {

          console.log(results);
          // convert the login data to a string
          var dataJSON = JSON.stringify(results);

        });

        // send the login data to the client webpage
        app.use('/', router);
      })
    }
  ])
});

这是否意味着router module 不起作用?

【问题讨论】:

  • 建议您将 Promise 链接在一起,而不是将它们嵌套在一起。此外,您不应将 app.get / app.post 嵌套在另一个 app.post 中。
  • @AndrewNolan 谢谢。我目前正在对其进行排序,并且似乎正在工作。我正在使用带有瀑布流和系列控制流的异步库
  • @AndrewNolan 我似乎无法在原始 app.post 之外获取 app.get。你知道有什么方法可以做到这一点吗?将 app.get 放入一个函数中是否会将其置于 app.post 之外?

标签: node.js mongodb express


【解决方案1】:

我在async series 的末尾添加了一个function,它将results 作为参数传递,以便将app.get 放在外面而不是嵌套它。

我也把它改成了app.get而不是app.use

这是 async series 的结尾,新的 function 被调用:

      function(err,results) {

        // call function to send login data to the client
        sendLoginData(results);
      })

这是新的function,它使用app.get向客户端发送数据:

// function to send login data to the client
function sendLoginData(data) {

  // send the login data to the client webpage
  app.get('/loginData', (req, res) => {
    res.json(data);
  });
};

【讨论】:

    猜你喜欢
    • 2015-09-29
    • 2022-12-13
    • 2016-07-21
    • 2015-02-10
    • 2014-09-07
    • 2019-05-27
    • 2013-02-01
    • 2017-05-07
    • 2016-02-23
    相关资源
    最近更新 更多