同一函数在两个不同的组件上返回两个不同的结果是否正常?

时间:2019-06-05 21:54:27

标签: react-native

我正在开发一个本机应用程序,该应用程序从Firestore接收数据。我将接收到的所有数据放入数组中,并在渲染之前使用长度进行测试。在屏幕A中,长度大于0,但在屏幕B中为0,但是两个数组都包含数据。

它尝试将代码从一页复制到另一页,但是什么也没复制,两个屏幕的结果相同

屏幕A

getData = async () => {
    const ref = await firebase
      .firestore()
      .collection("PlatPost")
      .where("active", "==", true);
    this.unsubscribe = await ref.onSnapshot(this.parseData);
    await this.setState({ loading: false });
  };

  parseData = async querySnapshot => {
    await querySnapshot.forEach(async doc => {
      let _data = await doc.data();
      let key = await doc.id;
      let normalDate = await this.convertToDate(_data.date.toDate());
      await this.data.push({ ..._data, key, normalDate });
    });
    await this.setState({ data: [...this.data] });
    await console.log(this.data) //Printing the result in console
  };

屏幕B

getData = async () => {
    const ref = await firebase
      .firestore()
      .collection("Notification")
      .where("recipient.id", "==", this.state.userId);
    this.unsubscribe = await ref.onSnapshot(this.parseData);
    await this.setState({ loading: false });
  };

  parseData = async querySnapshot => {
    await querySnapshot.forEach(async doc => {
      let _data = await doc.data();
      let key = await doc.id;
      let normalDate = await this.convertToDate(_data.date.toDate());
      await this.data.push({ ..._data, key, normalDate });
    });
    await this.setState({ data: [...this.data] });
    await console.log(this.data) //Priniting the result in console 
  };

屏幕A的结果

(3) [{…}, {…}, {…}]
0: {username: "K", active: true, …}
1: {username: "Cuisinier", active: true, …}
2: {username: "K", active: true, …}
length: 3
__proto__: Array(0)

我希望获得与屏幕A相同的结果,但在屏幕B上得到了

[]
0: {username: "hermann", active: true, …}
1: {username: "Test", active: true, …}
length: 0
__proto__: Array(0)

0 个答案:

没有答案
相关问题