Retrieving (allpost) data from firebase and using uid want to retrieve user information also

折月煮酒 提交于 2021-02-11 13:58:34

问题


I want retrieve all post then with each post there is a uid, using this uid i want to retrieve user information

My code is like this but it is not working and state is not changing in then block

getAllPost(){ 
    let allPost = firebase.database().ref("all-post");
    let postAll = [];
    allPost.once("value").then(data => {
      data.forEach(function (childSnapshot) {
        let childData = childSnapshot.val();
        childData.postId = childSnapshot.key;
        let userInfo = firebase.database().ref(`users/${childData.uid}`);
        userInfo.once("value").then(data => {
          childData.userInfo = data;
          postAll.push(childData);        
        })
      })
    }).then(()=>{
      this.setState({ allPost: postAll,loading: false  },);
    }).catch(err => {
      this.setState({ loading: false });
    });
  }

回答1:


As Josh commented, you're not handling the fact that all your userInfo.once() calls are asynchronous. This means that right now your this.setState({ allPost: postAll,loading: false },) call fires before the postAll.push(childData) happen. You should be able to easily see this by adding some console.log statements.

The solution is to wait until all the loading is finished, by using Promise.all():

let allPost = firebase.database().ref("all-post");
let postAll = [];
allPost.once("value").then(data => {
  let promises = [];
  data.forEach(function (childSnapshot) {
    let childData = childSnapshot.val();
    childData.postId = childSnapshot.key;
    let userInfo = firebase.database().ref(`users/${childData.uid}`);
    promises.push(
      userInfo.once("value").then(data => {
        childData.userInfo = data;
        postAll.push(childData);        
      })
    })
  })
  return Promise.all(promises);
}).then(()=>{
  this.setState({ allPost: postAll,loading: false },);
}).catch(err => {
  this.setState({ loading: false });
});


来源:https://stackoverflow.com/questions/60038757/retrieving-allpost-data-from-firebase-and-using-uid-want-to-retrieve-user-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!