无法从 firebase 数据库映射数据数组

Cannot map an array of data from firebase database

我正在制作一个血库应用程序,我遇到了一个问题,我无法在 React Native JSX 中映射我的数据列表。我从我之前在react native中制作的todoApp中复制了大部分代码并对其进行了修改。我还希望它在用户打开应用程序时自动获取所有数据,而不是手动使用按钮。

APP.JS:

    import React from 'react';
    import {Button,Text, View, ScrollView} from 'react-native';
    import firebase from 'firebase';
    
const firebaseConfig = {
  apiKey: "AIzaSyBPAEj0ku0YBF1DzCc1b6mGpEKz0Bhn9Fk",
  authDomain: "bloodbank-pro.firebaseapp.com",
  projectId: "bloodbank-pro",
  storageBucket: "bloodbank-pro.appspot.com",
  messagingSenderId: "533183192799",
  appId: "1:533183192799:web:82b1a608af84d64e6d536a",
  measurementId: "G-06F49XSLF4"
};
firebase.initializeApp(firebaseConfig);

    export default class App extends React.Component {
    
        constructor(props){
            super(props);
            this.state = {
                list: [],
            }
        }
          
        work=()=>{
            firebase.database().ref().once('value').then(snapshot => {
             var snap = snapshot.val();
             var newStateArray = this.state.list.slice();
             newStateArray.push({id: this.state.list.length + 1,username: snap.username, age: snap.age, bloodtype: snap.bloodtype, phonenumber: snap.phonenumber});
             this.setState({list: newStateArray,});
            });
        }  
    
        render(){
            return(<>
            <ScrollView style={{ backgroundColor: 'white', marginVertical:20 }} showsVerticalScrollIndicator={true}>
            <View>
              {this.state.list.map(item => (<>
                <View key={item.id}  style={{borderBottomColor: 'black',borderBottomWidth: 5, marginBottom: 20}}>
                  <Text style={{fontSize: 22}}>{item.username}, {item.age}, {item.phonenumber}, {item.bloodtype}</Text>
                </View>
              </>))}
            </View>
            </ScrollView>
                <View style={{position: 'absolute', bottom: 1, left: 1, right: 1,}}>
                <Button title="test" onPress={this.work} />
                <Button title="Go to My Profile" />
                </View>
                </>)
        }
    }

Firebase 数据库:

这是在 React Native CLI 中制作的。我将非常感谢你的帮助。

您的应用可能正在尝试在数据库完全初始化之前获取数据。最好的做法是将

const firebaseConfig = {
  apiKey: "AIzaSyBPAEj0ku0YBF1DzCc1b6mGpEKz0Bhn9Fk",
  authDomain: "bloodbank-pro.firebaseapp.com",
  projectId: "bloodbank-pro",
  storageBucket: "bloodbank-pro.appspot.com",
  messagingSenderId: "533183192799",
  appId: "1:533183192799:web:82b1a608af84d64e6d536a",
  measurementId: "G-06F49XSLF4"
};
firebase.initializeApp(firebaseConfig);

在您的项目 index.js 中。在您的 index.js 文件中写入

componentDidMount() {
  const firebaseConfig = {
  apiKey: "AIzaSyBPAEj0ku0YBF1DzCc1b6mGpEKz0Bhn9Fk",
  authDomain: "bloodbank-pro.firebaseapp.com",
  projectId: "bloodbank-pro",
  storageBucket: "bloodbank-pro.appspot.com",
  messagingSenderId: "533183192799",
  appId: "1:533183192799:web:82b1a608af84d64e6d536a",
  measurementId: "G-06F49XSLF4"
};
firebase.initializeApp(firebaseConfig);
}

App.js文件中写入:

componentDidMount(){
   this.work()
}