Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用react与cloudfirestore_Javascript_Reactjs_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 使用react与cloudfirestore

Javascript 使用react与cloudfirestore,javascript,reactjs,firebase,google-cloud-firestore,Javascript,Reactjs,Firebase,Google Cloud Firestore,我正试图找出如何让cloud firestore使用react应用程序 我发现,它使用react和实时数据库,并已将其加载。现在,我正试图弄清楚我需要做哪些更改才能让它与CloudFireStore一起工作 在我的firebase.js中,我有: import app from 'firebase/app'; import 'firebase/auth'; import 'firebase/firestore'; import firestore from "firebase/firestore

我正试图找出如何让cloud firestore使用react应用程序

我发现,它使用react和实时数据库,并已将其加载。现在,我正试图弄清楚我需要做哪些更改才能让它与CloudFireStore一起工作

在我的firebase.js中,我有:

import app from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import firestore from "firebase/firestore";

class Firebase {
  constructor() {
    app.initializeApp(config).firestore();
    this.auth = app.auth();
    // this.db = app.firebase.database()
    this.db = app.firebase.firestore();

  }  

    doCreateUserWithEmailAndPassword = (email, password) =>
      this.auth.createUserWithEmailAndPassword(email, password);  
    doSignInWithEmailAndPassword = (email, password) =>
      this.auth.signInWithEmailAndPassword(email, password);  
    doSignOut = () => 
      this.auth.signOut();
    doPasswordReset = email => 
      this.auth.sendPasswordResetEmail(email);
    doPasswordUpdate = password =>
      this.auth.currentUser.updatePassword(password);

    // *** User API ***
    user = uid => this.db.ref(`users/${uid}`);
    users = () => this.db.ref('users');  

}
export default Firebase;
然后,以我的形式,我试着:

import { withFirebase } from '../../../components/firebase';

onSubmit = event => {
    const { username, email, passwordOne } = this.state;

    this.props.firebase
      .doCreateUserWithEmailAndPassword(email, passwordOne)
      .then(authUser => {
        // Create a user 
        return this.props.firebase
          .user(authUser.user.uid)
          .set({
            username,
            email,
          });

      })
      .then(authUser => {
        this.setState({ ...INITIAL_STATE });
        this.props.history.push(ROUTES.INITIAL_PROFILE);
      })
      .catch(error => {
        this.setState({ error });
      });
      event.preventDefault();
  }
  onChange = event => {
    this.setState({ [event.target.name]: event.target.value });
  };
使用Firebase导入具有:

import FirebaseContext, { withFirebase } from './Context';
import Firebase from '../../firebase.1';
export default Firebase;
export { FirebaseContext, withFirebase };
FirebaseContext具有:

import React from 'react';
const FirebaseContext = React.createContext(null);

export const withFirebase = Component => props => (
  <FirebaseContext.Consumer>
    {firebase => <Component {...props} firebase={firebase} />}
  </FirebaseContext.Consumer>
);
export default FirebaseContext;
如何让firestore在react应用程序中工作?

更改此选项:

this.db = app.firebase.firestore();
为此:

this.db = app.firestore();
返回类型
App
,在
App
中可以找到方法
firestore()

更改此选项:

this.db = app.firebase.firestore();
为此:

this.db = app.firestore();
返回类型
App
,在
App
中可以找到方法
firestore()


app
实际上是你的firebase实例,所以我猜你必须做
app.firestore()
app
实际上是你的firebase实例,所以我猜你必须做
app.firestore()