如何解决从React Store的Firestore集合中获取价值
我想编写一个将获取集合值的函数,如图所示:
这是我的代码,我真的不知道在“ then()”之后该怎么做:
const getLocation = () => {
firebase
.firestore()
.collection("users")
.doc(currentUser.uid)
.get()
.then((querySnapshot) => {});
};
请注意,currentUser是redux,这意味着查询将仅针对登录的当前用户执行
解决方法
如果只想返回businessLocation
的值,则可以这样做:
const getLocation = () => {
return firebase.firestore()
.collection("users")
.doc(currentUser.uid)
.get()
.then(function(doc) {
if (doc.exists) {
data = doc.data();
return data.businessDetails.businessLocation;
} else {
return "";
}
});
};
您可以在Official Documentation上获取有关如何在Firestore中获取数据的更多信息。
注意:这使您的函数成为异步函数。因此,您应按以下方式调用它:
getLocation().then(result => {
//Do whatever you want with the result value
console.log(result);
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。