[英]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 中所示。
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.