javascript - 无法从多个集合 Firebase 中检索数据
问题描述
希望你身体健康。我有个问题。
export function fetchListing() {
return function (dispatch) {
dispatch(fetchListingRequest());
//Getting Listing where status is in_review
firebase
.firestore()
.collection("listings")
.where("status", "==", "in_review")
.onSnapshot(
snapshot => {
const data = [];
snapshot.docs.forEach(doc => {
const temp = {};
// Getting address of business
firebase
.firestore()
.collection("users")
.doc(doc.data().business_id)
.get()
.then(users => {
temp["address"] = users.data().address;
})
.catch(error => {
dispatch(fetchListingFailed(error));
});
temp["title"] = doc.data().title;
temp["description"] = doc.data().description;
temp["listing_file"] = doc.data().listing_file;
data.push([doc.id, temp]);
});
dispatch(fetchListingSucess(data));
},
error => {
dispatch(fetchListingFailed(error));
}
);
};
}
我无法在状态下获取地址,但是当我记录它时它显示在控制台中。当我从firebase检索地址时,我可以访问地址,并且在reducer中我也可以获得地址。
解决方案
原因可能是:您的 Firestore 集合的层次结构,或者您必须知道在 js 中,您不能将数据影响到主编中的变量,仅当它位于一级函数中时,您可以执行此操作
locale = this ;
比你可以使用其他两个级别的所有变量
推荐阅读
- javascript - Jinja 表,当列 == 值时,将 div 添加到特定列
- docker - Dockerfile 中的环境变量
- python - Python 创建列表列表
- css - 如何在离子选择选项标签中有 2 种颜色
- java - 使用 Java 在 Windows 机器上的 cmd 上运行 shell 命令
- react-native - 世博链接fb post url方案
- ajax - Ajax 参数始终为空
- elasticsearch - 在 ElasticSearch 中使用嵌套字段是否会扩展或有任何意外的陷阱?
- javascript - 有没有一种从邮政编码推断数字范围的好方法?
- amazon-web-services - DynamoDB 上聊天应用的单表设计,检查方向是否正确