【发布时间】:2020-07-09 10:44:24
【问题描述】:
我正在为一个项目构建一个 crud 应用程序, 人们可以添加查看和删除条目,我为此使用了 nodejs、js 和 fireabse。 我有这样一个 Firestore 数据库结构:
entries:
--entry id
--entry data
users:
--user id
--user email (query)
-- my entries (collection)
-- entries id
--entry id
现在我想显示所有用户条目, 所以我创建了这个 module.exports 函数:
module.exports = {
//get all the users entries from database with passed in uid ('/dashboard')
getUserEntries: async uid => {
//get all his entries docs id from "myEntries" collection in an, array
const usersEntriesId = await db
.collection("users")
.doc(uid)
.collection("myEntries")
.get()
.then(entries => {
return entries.docs.map(entry => entry.data().entry);
})
.catch(err => console.log(err));
console.log(usersEntriesId); //works fine, logs array with ids
const userEntriesDocs = usersEntriesId.map(async id => {
const entry = await db
.collection("entries")
.doc(id)
.get()
.then(entry => {
return entry.data();
});
console.log("hello:", entry); //works fine returns logs entry data
return entry;
});
console.log("hello1: ", userEntriesDocs); //doesnt work i get "hello1: [ Promise { <pending> },
// Promise { <pending> },
//Promise { <pending> } ]"
//i got three entries, that's why i get 3 times "Promise { <pending> }"
}
};
那我该如何解决呢?
谢谢
【问题讨论】:
-
这就是应该发生的事情。如果你想要一个 promise 解析的值,等待它或使用 .then。
标签: javascript node.js firebase promise async-await