Firebase.Firestore不是功能

时间:2020-02-21 18:43:30

标签: reactjs firebase react-native firebase-realtime-database google-cloud-firestore

我正在使用从Firebase实时数据库更改为Firebase Firestore的React Native应用程序,但是当我在Expo上运行该应用程序时,出现此错误: * TypeError:TypeError:firebase.firestore不是函数。 (在“ firebase” firestore()”中,“ firebase.firestore”未定义)

这是我的代码:

import React from 'react';
import {View, Text, StyleSheet, TouchableOpacity, FlatList, Button, Picker} from 'react-native';
import * as firebase from 'firebase';

export default class HomeScreen extends React.Component {

    state = {
        email: ""
    };

    //constructor that saves the states of string "student" and array "students" empty values
    constructor(props) {
        super(props)

        this.state = {
            student: '',
            students: [],
            selectedStudent: '',
            studentItem: ''
        }
    }

    //once page is initialized, the child table "students" of the root database location
    //(.ref().child("students")) will be read once, and if the "students" table exists in the database,
    //each value in that table will be stored as a "student" string, and populate the "students" array
    componentDidMount() {
        console.disableYellowBox = true;

        firebase.firestore().collection('Students').get().then((snapshot) => {
            snapshot.docs.forEach(doc => {
                console.log(doc);
            })
        });

        // firebase.database().ref().child("students").once("value", snapshot => {
        //  const data = snapshot.val()
        //  if(data) {
        //      const initStudents = [];
        //      Object.keys(data).forEach(student => initStudents.push(data[student]));
        //      this.setState({
        //          students: initStudents
        //      })
        //  }
        // });
    }

1 个答案:

答案 0 :(得分:0)

通过示例,它是我的Firestore模块的一部分-我尽可能从我的应用程序中提取许多Firestore详细信息:

import "@firebase/app";
import firebase from "@firebase/app";
import "@firebase/firestore";
import { config } from "../constants";

//////////////////////////////////////////////////////////////////////
// note schema is custom and local

firebase.initializeApp(config);
const fdb = firebase.firestore();
fdb.enablePersistence({ synchorizeTabs: true });

//////////////////////////////////////////////////////////////////////
// routines

export const fetchRecord = (tablePath, Id, ref = null, transaction = null) => {
  const db = ref ? ref : fdb;

  const docRef = db.collection(tablePath).doc(Id);

  return Promise.resolve()
    .then(() => {
      if (transaction) return transaction.get(docRef); // note early return
      else return docRef.get();
    })
    .then(docSnapshot => {
      if (docSnapshot.exists) {
        return Promise.resolve({
          ...docSnapshot.data(),
          Id: docSnapshot.id,
          ref: docSnapshot.ref
        });
      } else {
        return Promise.reject(null);
      }
    });
};