muklah
muklah

Reputation: 865

null returned when fetching data from firebase using expo

I'm trying to fetch data from firebase in my expo app so this is the component where I implement this code:

import firebase from 'firebase';

// Initialize Firebase
var config = {
    apiKey: "AIzaSyAVf",
    authDomain: "d07f5.firebaseapp.com",
    databaseURL: "https://d07f5.firebaseio.com",
    projectId: "d07f5",
    storageBucket: "d07f5.appspot.com",
    messagingSenderId: "66392528"
  };
  firebase.initializeApp(config);

const api = 
    firebase.database().ref('/Barbers').on('value', (snapshot) => {
        console.log('testing')
        console.log(snapshot.val())
    });

  export default {
    getBarbersShops,
  }

when I run the app on my android device and seeing the remote debugging from the console of browser I find the " testing " word that I write in console but for this console:

console.log(snapshot.val())

I just got null for it and can't understand why?

and this is image for collection in firebase:

enter image description here

Upvotes: 0

Views: 583

Answers (2)

Sean Stayns
Sean Stayns

Reputation: 4234

doug stevensons answer is totally right! To get the document from firestore in react native, you can use the following code:

import firebase from 'react-native-firebase'

myFunction(){
    firebase.firestore().collection('Barbers').doc('9R4t...').get().then(doc => {
       if(doc.exists){
          console.log(doc.data().name);  // testing
       }
    }
}

or async:

async myFunctionAsync(){
    const doc = await firebase.firestore().collection('Barbers').doc('9R4t...').get();

    if(doc.exists){
       console.log(doc.data().name);  // testing
    }
}

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317372

You're getting null because your code is accessing Firebase Realtime Database, but the picture of your data is showing it stored in Cloud Firestore. These are not the same products - they share neither the same data nor the same APIs.

Upvotes: 3

Related Questions