0

我正在尝试开发我的第一个 React Native 应用程序,但我被从 firestore 存储/获取数据所困扰。

问题是在从 firestore 获取所有数据之前调度,所以我的过滤配置文件数组是空的,我的屏幕中的平面列表也很空。

我应该修改什么?如果有人可以帮助我,我将不胜感激..谢谢!

这是我屏幕上的代码:

ProfilesListScreen.js

const ProfilesListScreen = props => {   
  const connectedUser = useSelector(state => state.profiles.user); 
  const filteredProfiles = useSelector(state => state.profiles.filteredProfiles)

    const dispatch = useDispatch();

    useEffect(() => {
      dispatch(fetchProfiles()); 
      }, [dispatch]);

我的profilesAction.js 在我的商店:

export const fetchProfiles= () => {

    
    const loadedProfiles = [ ];

    return async dispatch => {               
        

        await firebase.firestore().collection('users')
        .get()
        .then((profileSnapShot) => {            
            profileSnapShot.forEach((doc) => {               
                const firstname=  doc.get("firstname");
                const birth = doc.get("birth");
                const age = getAge(birth.seconds);
                const sex = doc.get("sex");                 
                const newProfile= new profile(doc.id, firstname,age, "https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
                
                loadedProfiles.push(newProfile); 
               
                
            }); 
        })

         dispatch({ type: SET_PROFILESLIST, profilesList: loadedProfiles } ) ;
        

    };

和我的减速器profiles.js

case SET_PROFILESLIST:
          const newProfilesList = action.profilesList;
           return { 
            filteredProfiles : newProfilesList,
           };

4

1 回答 1

0

尝试更新 fetchProfiles 的代码,如下所示,这可能会帮助您解决问题。

export const fetchProfiles= () => {
    const loadedProfiles = [];
    return async dispatch => {               
        const profileSnapShot = await firebase.firestore().collection('users').get()
        profileSnapShot.forEach((doc) => {               
            const firstname=  doc.get("firstname");
            const birth = doc.get("birth");
            const age = getAge(birth.seconds);
            const sex = doc.get("sex");                 
            const newProfile= new profile(doc.id, firstname,age, "https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
            loadedProfiles.push(newProfile); 
        });
        dispatch({ type: SET_PROFILESLIST, profilesList: loadedProfiles });
    }
};
于 2021-07-07T12:36:05.437 回答