【发布时间】:2020-09-26 19:01:49
【问题描述】:
我需要一些理解和解决方案。
我有一个单独的 js 文件,用于处理所有 api 调用。
所以我想在我的 HomeScreen 中从 Firestore 中获取数据。
在 then 调用的 Api 中,我得到了我的数据,但在主屏幕中,我只得到了一个 Promise。
我尝试了一些更改,但没有任何帮助。我不明白这个。 我如何在我的 HomeScreen 中获取数据?
我的代码有问题吗?
Api.js
export const getCurrentUserProfile = () => {
if (firebase.auth().currentUser.uid) {
const userDocID = firebase.auth().currentUser.uid;
const docRef = db.collection('Users').doc(userDocID);
docRef.get().then((doc) => {
console.log(firebase.auth().currentUser.uid);
if (doc.exists) {
console.log(doc.data()); // return the data i need
return doc.data();
} else {
console.log('No document for this ID: undefined');
}
}).catch((error) => {
console.log('Error getting document', error);
});
}
}
HomeScreen.js
const user = getCurrentUserProfile();
console.log(user);
// And here i get this back:
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}
const user = getCurrentUserProfile().then(data => console.log(data));
// this return as well undefined
非常感谢
【问题讨论】:
标签: javascript google-cloud-firestore promise