Как я могу создать пользовательскую базу данных в firebase с реагировать родной - PullRequest
0 голосов
/ 30 сентября 2019

У меня проблема с записью в базе данных. В этом приложении я хочу создать пользовательскую базу данных для каждого плоского числа в базе данных Firebase в реальном времени, т.е. я могу создать базу данных «Пользователи», но я хочу создать одну базу данных для каждого плоского номера, чтобы каждая запись получала питаниев этой конкретной базе данных.

class Entrylist extends React.Component{



  constructor(props) {
   super(props);

    this.state =({

    })
 }

 writeUserData(name,phoneno,flatno,purpose,vehicleno){
    firebase.database().ref('Users/').push({
        name,
        phoneno,
        flatno,
        purpose,
        vehicleno
    }).then((data)=>{
        //success callback
        console.log('data ' , data)
    }).catch((error)=>{
        //error callback
        console.log('error ' , error)
    })
}

// style = {marginTop: StatusBar.currentHeight}
  render(){


    return(

      <View style = {{flex : 1}}>
      <View style = {{...StyleSheet.absoluteFill}}>
      <Image
      style = {{flex : 1,height : null,width : null}}
      blurRadius= {2}
      source={{ uri: 'https://firebasestorage.googleapis.com/v0/b/newproj-7a868.appspot.com/o/wallpaper24.jpg?alt=media&token=86ea62ac-fff4-4178-93a5-59c96abc82e4'}}>
      </Image>
      </View>
     <View style = {{width : width,height : 50,alignItems : 'center',flexDirection : 'row',marginTop: 10}}>
     <View style = {{marginLeft : 20}}>
     <Icon
     onPress = {() => this.props.navigation.navigate('Signin')}
     name = 'arrow-back'
     size = {20}
     color = 'black'/>
     </View>
     <View style = {{alignSelf : 'center',left : width/4}}>
     <Text style = {{fontSize : 27}}>New Entry</Text>
     </View>
     </View>



    <View style = {{...styles.container,marginTop : 40}}>
    <ScrollView>
     <TextInput style = {styles.input}
      placeholder = "Enter Name"
      onChangeText = {(name) => this.setState({name})}
    />
    <TextInput style = {{...styles.input,marginTop : 10}}
    placeholder = "Enter Mobile number"
      onChangeText = {(phoneno) => this.setState({phoneno})}
    />
    <TextInput style = {{...styles.input,marginTop : 10}}
    placeholder = "Enter Flat number"
      onChangeText = {(Users) => this.setState({Users})}
    />

    <TextInput style = {{...styles.input,marginTop : 10}}
    placeholder = "Enter Purpose"
      onChangeText = {(purpose) => this.setState({purpose})}
    />
    <TextInput style = {{...styles.input,marginTop : 10}}
    placeholder = "Enter Vehicle number"
      onChangeText = {(vehicleno) => this.setState({vehicleno})}
    />
        </ScrollView>
        </View>
        <View>


        <View style = {styles.roundbutton}>
        <Button style = {{backgroundColor : 'white',elevation : 0}}
          onPress = {() => this.writeUserData(this.state.name, this.state.phoneno,this.state.flatno,this.state.purpose,this.state.vehicleno)}>
        <Icon
        name = 'md-chatbubbles'
        type = 'ionicon'
        size = {30}
        color = "black"
        />
        </Button>
        </View>


        <View style = {{elevation : 4,width : '100%',height : 70,backgroundColor : 'white',flexDirection : 'row',justifyContent : 'center',alignItems : 'center',borderColor : 'black'}} >
         <View style = {{flex : 0.5,width : '100%',height : '100%',alignItems:'center',justifyContent:'center',borderColor : 'black',borderWidth:0.5}}>
         <Button style = {{backgroundColor : 'white',elevation : 0}}
         onPress = {() => this.props.navigation.navigate('Menu')}>
         <Icon
         size = {30}
         name='dashboard'
        color='black'
         />
         </Button>
         </View>
        <View style = {{flex : 0.5,width : '100%',height : '100%',alignItems:'center',justifyContent:'center',borderColor : 'black',borderWidth:0.5}}>
          <Button style = {{backgroundColor : 'white',elevation : 0}}
          onPress = {() => this.props.navigation.navigate('Settings')}>
          <Icon

          name = "ios-settings"
          type='ionicon'
          size = {35}
          color = "black"
          />
          </Button>
         </View>
         </View>
         </View>
        </View>
    );
  }
}

export default Entrylist;

1 Ответ

0 голосов
/ 01 октября 2019

Это было легко ... все, что мне нужно было сделать, это firebase.database (). Ref ('Users /') здесь вместо 'Users /', я должен был заменить его на this.state.customdatabase, который былопределенный в конструкторе и при вводе текста я заменил его на this.setstate.customdatabase

...