如何使用react-Native中另一个集合的数据从Firestore中获取一个集合.....我在做什么错?

时间:2018-07-26 13:41:57

标签: react-native

我已经尝试了从stackoverflow到GitHub的所有地方搜索,但是我可以找到解决方案。我正在尝试使用我从一系列企业中获得的用户ID来获取用户列表。我在做什么错了?

componentWillMount() {
//Loading all the business collections.
firebase.firestore().collection("business").onSnapshot((snapshot) => {
  var bizs = [];
  snapshot.forEach((bdt) => {
    var userdt = [];
    //get document id of a certain user in the business collections
    firebase.firestore().collection('users').where("userid", "==", bdt.data().userid).get()
      .then((snap) => {
        snap.forEach(dc => {
          //loading details of the user from a specific ID
          firebase.firestore().collection("users").doc(dc.id).onSnapshot((udt) => {
            userdt.push({
              name: udt.data().fullname,
              photourl: udt.data().photoURL,
              location: bdt.data().location,
              openhrs: bdt.data().openHrs,
              likes: '20',
              reviews: '3002',
              call: bdt.data().contacts
            });
            console.log(userdt); //this one works
          })
          console.log(userdt); // but this one doesnt diplay anything just []
        })
      }).catch((dterr) => {
        console.log(dterr)
    })
  });
  this.setState({bizdata: bizs,loading: false
  });
});
}

我正在使用react-native和firestore

1 个答案:

答案 0 :(得分:0)

输入一些数字的日志,

       console.log('1',userdt);
       console.log('2',userdt);

检查天气中最先出现的天气,也许在更新数据之前正在执行“ 2”