Mel
Mel

Reputation: 2687

React with Cloud Firestore

Im trying to switch from realtime database to cloud firestore in my react app.

In my firebase.js, I have a definition of users as set out below.

class Firebase {
  constructor() {
    app.initializeApp(config).firestore();
    this.auth = app.auth();
    // this.db = app.firebase.database()
    this.db = app.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');  

}

This worked to get users when I was using realtime database, but now I get an error message that says:

this.db.ref is not a function

Why can't I reference this.db anymore and how do I navigate the cloud firestore docs to find the equivalent?

I have seen this post which recommends keeping the realtime database for some things. Is there a list of things that cloud firestore can't do and is getting a list of users one of them?

I have seen this documentation and tried:

user = uid => this.db.DocumentReference(`users/${uid}`);
    users = () => this.db.DocumentReference('users');  

This produces the same error message as using this.db.ref

Upvotes: 0

Views: 288

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

There is no ref() in firestore, if you want to access a document you need to use the method document():

db.collection("users").add({
    first: "Ada",
    last: "Lovelace",
    born: 1815
})
.then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});

Please check the following doc for firestore :

https://firebase.google.com/docs/firestore/quickstart

Upvotes: 2

Related Questions