【发布时间】:2020-05-22 18:50:46
【问题描述】:
如何向数据库发出多个请求,等待所有请求的结果并返回结果? 我尝试使用 Promise.all,但我得到的只是控制台中的 [undefined]。
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("DBusers");
var query = {};
var U4 = dbo.collection("users");
const promises = [
U4.find({}).count(function (err, result) { return result; }),
U4.find({}).count(function (err, result) { return result; })
];
Promise.all(promises).then(function (results) {
console.log(results);
}).catch(function (err) {
console.log(err);
});
db.close();
});
【问题讨论】: