node.js - Node.js服务器/快递模块不向客户端发送数据
问题描述
更新
根据@AndrewNolan 的建议,我将代码更改为使用async waterfall
和async series
来自的承诺链:
https://caolan.github.io/async/v3/docs.html#waterfall
https://caolan.github.io/async/v3/docs.html#series
被识别没有问题,app.use
但没有任何东西发送给客户。
内部不记录任何内容,但外部记录console.log
。router.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
它不起作用?
解决方案
我function
在末尾添加了一个 in作为参数async series
传递results
,以便获取app.get
外部而不是嵌套它。
我也将其更改为app.get
而不是app.use
这是调用async series
new的结尾: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);
});
};
推荐阅读
- docker - Docker联网,百思不得其解
- php - 带有 cakePHP 后端的 VueJS SPA 定期给出超时
- javascript - 如何通过 javascript 对象列表中的键值删除重复项?
- angularjs - 在 AngularJs 中设置动态范围变量
- scroll - 防止在模型更新Angular 8时滚动到顶部
- javascript - HTML如何让用户输入粗体或斜体?
- c++ - 为 Teseo-LIV3F 和 Nucelo-f401re 导入 hello world 程序时出现线程错误
- javascript - $uibModal 解除问题。然后不是函数
- biztalk - BizTalk 2013 R2 - 决策形状不遵循正确的逻辑路径
- python - 验证数据可以成为 tensorflow.keras 2.0 中的生成器吗?