【问题标题】:null returned when fetching data from firebase using expo使用 expo 从 firebase 获取数据时返回 null
【发布时间】:2019-01-07 06:25:09
【问题描述】:

我正在尝试在我的 expo 应用程序中从 firebase 获取数据 所以这是我实现此代码的组件:

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,
  }

当我在我的 android 设备上运行应用程序并从浏览器控制台查看远程调试时,我发现我在控制台中写的“测试”字样,但对于这个控制台:

console.log(snapshot.val())

我只是得到了 null 并且不明白为什么?

这是用于在 firebase 中收集的图像:

【问题讨论】:

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


    【解决方案1】:

    您的代码正在访问 Firebase 实时数据库,但您的数据图片显示它存储在 Cloud Firestore 中。它们不是同一种产品——它们既不共享相同的数据,也不共享相同的 API。

    【讨论】:

      【解决方案2】:

      道格史蒂文森的回答是完全正确的!要在 react native 中从 firestore 获取文档,可以使用以下代码:

      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
             }
          }
      }
      

      或异步:

      async myFunctionAsync(){
          const doc = await firebase.firestore().collection('Barbers').doc('9R4t...').get();
      
          if(doc.exists){
             console.log(doc.data().name);  // testing
          }
      }
      

      【讨论】:

      • 感谢您提供的示例,它非常有用@Sean Stayn
      猜你喜欢
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多