I am writing a background that encapsulates two methods to query the database, both of which return Promise, calls and both have the correct results.
function list(req, res, next) {
let start = req.query.start;
let count = req.query.count;
exchangeDb.queryList(start, count).then(results => {
res.json(results)
}).catch(err => {
throw err;
});
}
function load(req, res, next, name) {
let limit = req.query.limit;
exchangeDb.queryVolume(name, limit).then(results => {
req.results = util.convert(results);
return next();
}).catch(err => {
throw err;
});
}
the first one returns the list, and the second returns the details. I want to combine them into one method and try to do this:
function list(req, res, next) {
let start = req.query.start;
let count = req.query.count;
exchangeDb.queryList(start, count).then(results => {
results.forEach((index,element) => {
exchangeDb.queryVolume(element.name,50).then(volume => {
element.volume = volume;
results[index] = element;
})
});
res.json(results)
}).catch(err => {
throw err;
});
}
obviously, it doesn"t work. I am deeply troubled by this problem. If chained Promise,then (). Then () doesn"t seem to solve this problem, please take a look at it. Thank you.