【发布时间】:2020-03-17 13:49:12
【问题描述】:
我在从 MongoDB 集合中获取数据并将其返回到 NodeJS 代码中的数组时遇到问题。
到目前为止的代码返回了一个承诺,我不完全知道如何处理。
任何帮助将不胜感激。
async function loadData() {
try {
// Connect to the MongoDB cluster
console.log('Attempting to connect to DB')
client.connect(async err => {
const collection = client.db("db").collection("collection");
await collection.find({}).toArray().then((data) => {
//Getting stumped as the data gets returned as a promise and does not get added to an array
}
);
client.close();
console.log('Closed DB connection');
});
} catch (e) {
console.error(e);
}
【问题讨论】:
标签: arrays node.js mongodb promise