Milena -
Milena -

Reputation: 27

Can't save data in the Firebase database - React Native

I have already installed and import firebase in the project, the code looks like this:

componentWillMount(){
  var config = {
    apiKey: "***************************",
    authDomain: "configuracaofirebase-3a6ef.firebaseapp.com",
    databaseURL: "https://configuracaofirebase-3a6ef.firebaseio.com",
    projectId: "configuracaofirebase-3a6ef",
    storageBucket: "configuracaofirebase-3a6ef.appspot.com",
    messagingSenderId: "939162871117"
  };

  firebase.initializeApp(config);
}

salvarDados(){
  var database = firebase.database();
  database.ref("pontuacao").set("100");
}

render() {
  return (
    <View style={styles.container}>
      <Button 
        onPress={ () => { this.salvarDados() } }
        title="Salvar dados"
        color="#841584"
        accessibilityLabel="Salvar dados"
      />
      <Text>Meu App</Text>
    </View>
  );
}

does not work when I call the "salvarDados" method

show this error:

typeError: undifined is not a function (evaluating '_app.default.database()')

Upvotes: 0

Views: 479

Answers (2)

Milena -
Milena -

Reputation: 27

I solved it just by adding a new import:

import firebase from '@firebase/app';
import '@firebase/database';

Upvotes: 1

marco.marinangeli
marco.marinangeli

Reputation: 901

Try to remove the {} around the function call:

onPress={ () => { this.salvarDados() } 

Will become:

onPress={ () => this.salvarDados() }

Upvotes: 0

Related Questions