首页 > 解决方案 > 无法从多个集合 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中我也可以获得地址。

标签: javascriptreactjsreduxreact-redux

解决方案


原因可能是:您的 Firestore 集合的层次结构,或者您必须知道在 js 中,您不能将数据影响到主编中的变量,仅当它位于一级函数中时,您可以执行此操作

locale = this ;

比你可以使用其他两个级别的所有变量


推荐阅读