无法从React-Native-Firebase(v6)Firestore获取数据:undefined不是一个函数(在'... this._firestore.native.collectionGet ...附近)

时间:2019-11-29 12:19:24

标签: javascript firebase react-native google-cloud-firestore react-native-firebase

我在这个问题上困扰了很长时间。我刚刚开始使用react-native-firebase在我的react-native应用程序中实现Firestore。我只是关注文档[https://invertase.io/oss/react-native-firebase/v6/firestore/quick-start#reading-data],但对我而言不起作用。

这是在Android中。尚未在iOS中进行测试。

我不断收到此错误:

  

[TypeError: undefined is not a function (near '...this._firestore.native.collectionGet...')]

以下是相关代码:

import React, {Component} from 'react';
import { firebase } from '@react-native-firebase/firestore';

export default App extends Component{
  constructor(props) {
    super(props);

    this.getData= this.getData.bind(this)
    this.getData()

    this.state = {};
  }

  async getData() {
    try {
      const querySnapshot = await firebase.firestore()
      .collection('Gyms')
      .get() //error with this

      console.log('Documents', querySnapshot.docs);

    } catch (e) {
      console.log(e);
    }
  }
}


任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

发生此错误是因为缺少本机RNFirestore模块。

yarn @react-native-firebase/firestore之后,您需要运行pod install并使用react-native run-ios触发重建。

答案 1 :(得分:1)

如果您的Firebase / Firestore设置良好,那是因为您的查询为假,则可以使用以下方法进行测试:

import firestore from '@react-native-firebase/firestore';

firestore()
  .collection('collection')
  .onSnapshot((querySnapshot) => {
     console.log(querySnapshot)
  })