【发布时间】:2020-08-26 20:52:27
【问题描述】:
更新
根据@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 被识别没有问题,但没有发送给客户端。
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 之外?