首页 > 解决方案 > Node.js服务器/快递模块不向客户端发送数据

问题描述

更新

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

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

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

被识别没有问题,app.use但没有任何东西发送给客户。

内部不记录任何内容,但外部记录console.logrouter.get

// 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它不起作用?

标签: node.jsmongodbexpress

解决方案


function在末尾添加了一个 in作为参数async series传递results,以便获取app.get外部而不是嵌套它。

我也将其更改为app.get而不是app.use

这是调用async seriesnew的结尾: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);
  });
};

推荐阅读