简体   繁体   English

无法从 React Native 中的 Firebase 实时数据库读取数据

[英]Unable to read data from Firebase Realtime Database in React Native

I am working on a simple React Native app to read and write data from a Firebase database.我正在开发一个简单的 React Native 应用程序来从 Firebase 数据库读取和写入数据。 My read and write permissions in Firebase have been set to true:我在 Firebase 中的读写权限已设置为 true:

{
  "rules": {
    ".read": true,  
    ".write": true,  
  }
}

Here are my relevant files:这是我的相关文件:

App.js应用程序.js

import React from 'react'
import {View, StyleSheet, Button} from 'react-native'
import * as firebase from 'firebase'
import RootStackNavigator from './navigation/RootNavigation'

export default class App extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
      isLoadingComplete: false
    }
    var firebaseConfig = {
      apiKey: "xxxxxxx",
      authDomain: "testproject-9d0bc.firebaseapp.com",
      databaseURL: "https://testproject-9d0bc-default-rtdb.firebaseio.com",
      projectId: "testproject-9d0bc",
      storageBucket: "testproject-9d0bc.appspot.com",
      messagingSenderId: "1003049293166",
      appId: "1:1003049293166:web:1df37fd6d181cf895cdd7f"
    };

    if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig)
    } else {
      firebase.app()
    }
   }

  render() {
    return (
    <View style={styles.container}>
      <RootStackNavigator/>
    </View>
    )
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
  },
});

RootStackNavigation.js RootStackNavigation.js

import React from 'react';
import { createAppContainer, StackNavigator } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import HomeScreen from '../src/screens/HomeScreen';

const RootStackNavigator =  createStackNavigator(
  {
      screen: HomeScreen
  },
  {
    initialRouteName:"screen", // first component that should be displayed
    defaultNavigationOptions: {
      title: "App" 
    }
  }
);

export default createAppContainer(RootStackNavigator)

HomeScreen.js HomeScreen.js

import React from "react";
import { Text, StyleSheet, View, Button, TouchableOpacity } from "react-native";
import * as firebase from "firebase";
const HomeScreen = (
  props
) => {
  function getData() {
    firebase
      .database()
      .ref("people/")
      .on("value", (snapshot) => {
        const age = snapshot.val().age;
        console.log("Age: " + age);
      });
  }

  return (
    <View>
      <Text>Hello this is the home screen</Text>
      <Button title="Get Data" onPress={getData} />
    </View>
  );
};

const styles = StyleSheet.create({
  text: {
    fontSize: 30,
  },
});

export default HomeScreen;

My firebase database looks like this (all data was manually added through the firebase website)and I want to be able to print out these items after pressing the "Get Data" button to the console as shown in my getData function in HomeScreen.js我的 firebase 数据库看起来像这样(所有数据都是通过 firebase 网站手动添加的),我希望能够在按“获取数据”按钮到控制台后打印出这些项目,如HomeScreen.js中的getData ZC1C425268E68394D1AB45 中所示。

Firebase 数据库

However, my code in getData does not work and nothing prints out to my console.但是,我在getData中的代码不起作用,并且没有任何内容打印到我的控制台。 What am I missing here?我在这里想念什么?

Kindly modify this in your code请在您的代码中修改它

firebase.database.ref("people").on("value", snapshots => {
    let peoples = [];
    snapshots.forEach((snapshot) => {
   peoples.push(snapshot.val().age);
    console.log(snapshot.val().age);
    });
   // here you can set this Array
  // setPeople(peoples) 

Figured out my issue.想通了我的问题。 As you can see in my firebase database, the way I was accessing my data was wrong.正如您在我的 firebase 数据库中看到的,我访问数据的方式是错误的。 If I wanted to get the age of "dad", I should have called snapshot.val().dad .如果我想得到“爸爸”的年龄,我应该打电话给snapshot.val().dad

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用钩子从 react-native 中的 firebase 实时数据库中读取数据 - how do i read data from firebase realtime database in react-native using hooks 从 firebase 实时数据库中获取数据时如何通过 function 返回数据 - how to return data through function when fetching data from firebase realtime-database in react native 如何使用 React Native 从 Firebase 实时数据库中读取数据? - How to read data from Firebase Real Time database with React Native? 如何在 React Native 中将 Firebase 实时数据库数据值加一? - How to increment Firebase Realtime database data values by one in React Native? 渲染来自 firebase 实时数据库的 FlatList React Native(expo cli)中的数据? - render data in FlatList React Native (expo cli) from firebase realtime database? 如何从 Firebase 实时数据库中获取数据,这些数据是随机密钥的子项 - 使用本机反应? - How do I fetch data from firebase realtime database which are children to a random key - using react native? 如何从 firebase 实时数据库中呈现 ScrollView React Native (expo cli) 中的数据? - How to render data in ScrollView React Native (expo cli) from firebase realtime database? 将 Firebase 实时数据库与 React Native 应用程序集成 - Integrating Firebase Realtime Database with React Native App 使用本机反应更新 firebase 实时数据库中的 2 个子项 - Update 2 children in firebase realtime database with react native 无法将数据从Firebase实时数据库检索到Web应用程序 - Unable to retrieve data from firebase realtime database into web app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM