带有React Native的Firebase SDK无法看到任何内容

时间:2018-11-20 23:34:48

标签: firebase react-native

我正在Firestore的RN上关注此tutorial。到目前为止,我只使用通过

安装的Firebase Web SDK
npm install firebase -save

使用以下示例代码:

constructor(props) {
    super(props);
   this.ref = firebase.firestore().collection('sessions');

   this.unsubscribe = null;
   this.state = {
      dataSource: [],
      loading: true,
    };
  }

  componentDidMount() {

   this.unsubscribe = this.ref.onSnapshot(this.onCollectionUpdate);

  }

   componentWillUnmount() {
      his.unsubscribe();
   }

  onCollectionUpdate = (querySnapshot) => {
     const dataSource = [];
     querySnapshot.forEach((doc) => {

     const { id, desc, zipcode, timestamp } = doc.data();
       dataSource.push({
         key: doc.id,
         doc,
         desc,
         zipcode,
        timestamp,
      });
    });
    this.setState({
       dataSource,
       loading: false,
    });
 }

即使我输入了虚假的集合名称,上面的代码也绝对不会返回任何内容。什么都没运行,我放了一堆console.log语句,但仍然看不到任何东西。我什至不知道连接到Firestore是否有任何问题。

我还没有尝试过react-native-firebase模块,因为我认为我只是在做一个简单的Firestore查询,但是与此同时,我是在Mac上的iOS上本地构建应用程序的。

我应该使用react-native-firebase模块吗?

2 个答案:

答案 0 :(得分:0)

您的componentWillMount中有一个拼写错误。 componentWillUnmount() { his.unsubscribe(); }

应为:componentWillUnmount() { this.unsubscribe(); }

我还建议使用react-native-firebase。

答案 1 :(得分:0)

因此,对于任何发现类似问题的人,只想确认Firebase Web SDK确实可以用于Firestore。如果您的用例是非常简单的CRUD,则无需使用react-native-firebase。

我的错误是仅在我的渲染功能未显示数据库内容的情况下才拉数据库内容。