Delete Item by Key, Firebase React Native












0














I have a simple Notes React Native app and I am able to add and get data to it, but I am not sure how to remove/update data. The main problem is in getting the part where I tell firebase which data to remove. How can I pass a firebase key to a 'delete' function that takes the key as parameter and remove it from firebase.
I'm an absolute beginner at React Native, my code is the following:



export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
all_notitas: ,
notita_text: ''
};
};

componentWillMount() {
const notitasRef = firebase.database().ref('notitas');
this.listenForNotitas(notitasRef);
};

listenForNotitas = (notitasRef) => {
notitasRef.on('value', (dataSnapshot) => {
var aux = ;
dataSnapshot.forEach((child) => {
aux.push({
date: child.val().date,
notita: child.val().notita
});
});
this.setState({all_notitas: aux});
});
}; // listenForNotitas

render() {
let show_notitas = this.state.all_notitas.map((val, key) => {
return (
<Notita
key={key}
keyval={key}
val={val}
eventDeleteNotita={()=>this.deleteNotita(key)}> // I THINK THIS IS THE PROBLEM
</Notita>
);
});

return (
<View style={styles.container}>

<View style={styles.header}>
<Text style={styles.headerText}>NOTITAS</Text>
</View>

<ScrollView style={styles.scrollContainer}>
{show_notitas}
</ScrollView>

<View style={styles.footer}>
<TouchableOpacity
style={styles.addButton}
onPress={this.addNotita.bind(this)}>
<Text style={styles.addButtonText}>+</Text>
</TouchableOpacity>

<TextInput
style={styles.textInput}
placeholder='>>> Escribir notita'
placeholderTextColor='white'
underlineColorAndroid='transparent'
onChangeText={(notita_text) => (this.setState({notita_text}))}
value={this.state.notita_text}>

</TextInput>
</View>

</View>
);
}

addNotita() {
if (this.state.notita_text) {
var d = new Date();
dataForPush = {
'date': d.getDate() + '-' + d.getMonth() + '-' + d.getFullYear(),
'notita': this.state.notita_text
};
firebase.database().ref('notitas').push(dataForPush);
this.state.all_notitas.push(dataForPush);
this.setState(
{
all_notitas: this.state.all_notitas,
notita_text: '', // Limpiar input
}
)
} // end if
} // addNotita


When I do 'console.log(key)', it returns an int like 0, 1, 2, etc. It should return a firebase key like '-LRtghw8CjMsftSAXMUg' for example. I don't know what I am doing wrong and how to fix it.



deleteNotita(key) {
firebase.database().ref('notitas').child('' + key).remove()

/*
let updates = {};
console.log(key);
console.log(updates['/notitas/' + key]);
updates['/notitas/' + key] = null;
firebase.database().ref().update(updates); */

this.state.all_notitas.splice(key, 1);
this.setState({all_notitas: this.state.all_notitas});
} // deleteNotita

}









share|improve this question





























    0














    I have a simple Notes React Native app and I am able to add and get data to it, but I am not sure how to remove/update data. The main problem is in getting the part where I tell firebase which data to remove. How can I pass a firebase key to a 'delete' function that takes the key as parameter and remove it from firebase.
    I'm an absolute beginner at React Native, my code is the following:



    export default class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    all_notitas: ,
    notita_text: ''
    };
    };

    componentWillMount() {
    const notitasRef = firebase.database().ref('notitas');
    this.listenForNotitas(notitasRef);
    };

    listenForNotitas = (notitasRef) => {
    notitasRef.on('value', (dataSnapshot) => {
    var aux = ;
    dataSnapshot.forEach((child) => {
    aux.push({
    date: child.val().date,
    notita: child.val().notita
    });
    });
    this.setState({all_notitas: aux});
    });
    }; // listenForNotitas

    render() {
    let show_notitas = this.state.all_notitas.map((val, key) => {
    return (
    <Notita
    key={key}
    keyval={key}
    val={val}
    eventDeleteNotita={()=>this.deleteNotita(key)}> // I THINK THIS IS THE PROBLEM
    </Notita>
    );
    });

    return (
    <View style={styles.container}>

    <View style={styles.header}>
    <Text style={styles.headerText}>NOTITAS</Text>
    </View>

    <ScrollView style={styles.scrollContainer}>
    {show_notitas}
    </ScrollView>

    <View style={styles.footer}>
    <TouchableOpacity
    style={styles.addButton}
    onPress={this.addNotita.bind(this)}>
    <Text style={styles.addButtonText}>+</Text>
    </TouchableOpacity>

    <TextInput
    style={styles.textInput}
    placeholder='>>> Escribir notita'
    placeholderTextColor='white'
    underlineColorAndroid='transparent'
    onChangeText={(notita_text) => (this.setState({notita_text}))}
    value={this.state.notita_text}>

    </TextInput>
    </View>

    </View>
    );
    }

    addNotita() {
    if (this.state.notita_text) {
    var d = new Date();
    dataForPush = {
    'date': d.getDate() + '-' + d.getMonth() + '-' + d.getFullYear(),
    'notita': this.state.notita_text
    };
    firebase.database().ref('notitas').push(dataForPush);
    this.state.all_notitas.push(dataForPush);
    this.setState(
    {
    all_notitas: this.state.all_notitas,
    notita_text: '', // Limpiar input
    }
    )
    } // end if
    } // addNotita


    When I do 'console.log(key)', it returns an int like 0, 1, 2, etc. It should return a firebase key like '-LRtghw8CjMsftSAXMUg' for example. I don't know what I am doing wrong and how to fix it.



    deleteNotita(key) {
    firebase.database().ref('notitas').child('' + key).remove()

    /*
    let updates = {};
    console.log(key);
    console.log(updates['/notitas/' + key]);
    updates['/notitas/' + key] = null;
    firebase.database().ref().update(updates); */

    this.state.all_notitas.splice(key, 1);
    this.setState({all_notitas: this.state.all_notitas});
    } // deleteNotita

    }









    share|improve this question



























      0












      0








      0


      1





      I have a simple Notes React Native app and I am able to add and get data to it, but I am not sure how to remove/update data. The main problem is in getting the part where I tell firebase which data to remove. How can I pass a firebase key to a 'delete' function that takes the key as parameter and remove it from firebase.
      I'm an absolute beginner at React Native, my code is the following:



      export default class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      all_notitas: ,
      notita_text: ''
      };
      };

      componentWillMount() {
      const notitasRef = firebase.database().ref('notitas');
      this.listenForNotitas(notitasRef);
      };

      listenForNotitas = (notitasRef) => {
      notitasRef.on('value', (dataSnapshot) => {
      var aux = ;
      dataSnapshot.forEach((child) => {
      aux.push({
      date: child.val().date,
      notita: child.val().notita
      });
      });
      this.setState({all_notitas: aux});
      });
      }; // listenForNotitas

      render() {
      let show_notitas = this.state.all_notitas.map((val, key) => {
      return (
      <Notita
      key={key}
      keyval={key}
      val={val}
      eventDeleteNotita={()=>this.deleteNotita(key)}> // I THINK THIS IS THE PROBLEM
      </Notita>
      );
      });

      return (
      <View style={styles.container}>

      <View style={styles.header}>
      <Text style={styles.headerText}>NOTITAS</Text>
      </View>

      <ScrollView style={styles.scrollContainer}>
      {show_notitas}
      </ScrollView>

      <View style={styles.footer}>
      <TouchableOpacity
      style={styles.addButton}
      onPress={this.addNotita.bind(this)}>
      <Text style={styles.addButtonText}>+</Text>
      </TouchableOpacity>

      <TextInput
      style={styles.textInput}
      placeholder='>>> Escribir notita'
      placeholderTextColor='white'
      underlineColorAndroid='transparent'
      onChangeText={(notita_text) => (this.setState({notita_text}))}
      value={this.state.notita_text}>

      </TextInput>
      </View>

      </View>
      );
      }

      addNotita() {
      if (this.state.notita_text) {
      var d = new Date();
      dataForPush = {
      'date': d.getDate() + '-' + d.getMonth() + '-' + d.getFullYear(),
      'notita': this.state.notita_text
      };
      firebase.database().ref('notitas').push(dataForPush);
      this.state.all_notitas.push(dataForPush);
      this.setState(
      {
      all_notitas: this.state.all_notitas,
      notita_text: '', // Limpiar input
      }
      )
      } // end if
      } // addNotita


      When I do 'console.log(key)', it returns an int like 0, 1, 2, etc. It should return a firebase key like '-LRtghw8CjMsftSAXMUg' for example. I don't know what I am doing wrong and how to fix it.



      deleteNotita(key) {
      firebase.database().ref('notitas').child('' + key).remove()

      /*
      let updates = {};
      console.log(key);
      console.log(updates['/notitas/' + key]);
      updates['/notitas/' + key] = null;
      firebase.database().ref().update(updates); */

      this.state.all_notitas.splice(key, 1);
      this.setState({all_notitas: this.state.all_notitas});
      } // deleteNotita

      }









      share|improve this question















      I have a simple Notes React Native app and I am able to add and get data to it, but I am not sure how to remove/update data. The main problem is in getting the part where I tell firebase which data to remove. How can I pass a firebase key to a 'delete' function that takes the key as parameter and remove it from firebase.
      I'm an absolute beginner at React Native, my code is the following:



      export default class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      all_notitas: ,
      notita_text: ''
      };
      };

      componentWillMount() {
      const notitasRef = firebase.database().ref('notitas');
      this.listenForNotitas(notitasRef);
      };

      listenForNotitas = (notitasRef) => {
      notitasRef.on('value', (dataSnapshot) => {
      var aux = ;
      dataSnapshot.forEach((child) => {
      aux.push({
      date: child.val().date,
      notita: child.val().notita
      });
      });
      this.setState({all_notitas: aux});
      });
      }; // listenForNotitas

      render() {
      let show_notitas = this.state.all_notitas.map((val, key) => {
      return (
      <Notita
      key={key}
      keyval={key}
      val={val}
      eventDeleteNotita={()=>this.deleteNotita(key)}> // I THINK THIS IS THE PROBLEM
      </Notita>
      );
      });

      return (
      <View style={styles.container}>

      <View style={styles.header}>
      <Text style={styles.headerText}>NOTITAS</Text>
      </View>

      <ScrollView style={styles.scrollContainer}>
      {show_notitas}
      </ScrollView>

      <View style={styles.footer}>
      <TouchableOpacity
      style={styles.addButton}
      onPress={this.addNotita.bind(this)}>
      <Text style={styles.addButtonText}>+</Text>
      </TouchableOpacity>

      <TextInput
      style={styles.textInput}
      placeholder='>>> Escribir notita'
      placeholderTextColor='white'
      underlineColorAndroid='transparent'
      onChangeText={(notita_text) => (this.setState({notita_text}))}
      value={this.state.notita_text}>

      </TextInput>
      </View>

      </View>
      );
      }

      addNotita() {
      if (this.state.notita_text) {
      var d = new Date();
      dataForPush = {
      'date': d.getDate() + '-' + d.getMonth() + '-' + d.getFullYear(),
      'notita': this.state.notita_text
      };
      firebase.database().ref('notitas').push(dataForPush);
      this.state.all_notitas.push(dataForPush);
      this.setState(
      {
      all_notitas: this.state.all_notitas,
      notita_text: '', // Limpiar input
      }
      )
      } // end if
      } // addNotita


      When I do 'console.log(key)', it returns an int like 0, 1, 2, etc. It should return a firebase key like '-LRtghw8CjMsftSAXMUg' for example. I don't know what I am doing wrong and how to fix it.



      deleteNotita(key) {
      firebase.database().ref('notitas').child('' + key).remove()

      /*
      let updates = {};
      console.log(key);
      console.log(updates['/notitas/' + key]);
      updates['/notitas/' + key] = null;
      firebase.database().ref().update(updates); */

      this.state.all_notitas.splice(key, 1);
      this.setState({all_notitas: this.state.all_notitas});
      } // deleteNotita

      }






      javascript firebase react-native firebase-realtime-database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 19:44









      Frank van Puffelen

      226k27368395




      226k27368395










      asked Nov 22 at 18:55









      Lucian Jarpug

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You're dropping the key from Firebase when you're adding the notes to aux. The solution is to also keep the key from Firebase in there:



          notitasRef.on('value', (dataSnapshot) => {
          var aux = ;
          dataSnapshot.forEach((child) => {
          aux.push({
          date: child.val().date,
          notita: child.val().notita,
          id: child.key
          });
          });
          this.setState({all_notitas: aux});
          }


          And then pass that value to deleteNotita with:



          this.deleteNotita(val.id)





          share|improve this answer





















          • Just tried it and it worked. I'm feeling dumb, thank you.
            – Lucian Jarpug
            Nov 22 at 20:14











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436746%2fdelete-item-by-key-firebase-react-native%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You're dropping the key from Firebase when you're adding the notes to aux. The solution is to also keep the key from Firebase in there:



          notitasRef.on('value', (dataSnapshot) => {
          var aux = ;
          dataSnapshot.forEach((child) => {
          aux.push({
          date: child.val().date,
          notita: child.val().notita,
          id: child.key
          });
          });
          this.setState({all_notitas: aux});
          }


          And then pass that value to deleteNotita with:



          this.deleteNotita(val.id)





          share|improve this answer





















          • Just tried it and it worked. I'm feeling dumb, thank you.
            – Lucian Jarpug
            Nov 22 at 20:14
















          1














          You're dropping the key from Firebase when you're adding the notes to aux. The solution is to also keep the key from Firebase in there:



          notitasRef.on('value', (dataSnapshot) => {
          var aux = ;
          dataSnapshot.forEach((child) => {
          aux.push({
          date: child.val().date,
          notita: child.val().notita,
          id: child.key
          });
          });
          this.setState({all_notitas: aux});
          }


          And then pass that value to deleteNotita with:



          this.deleteNotita(val.id)





          share|improve this answer





















          • Just tried it and it worked. I'm feeling dumb, thank you.
            – Lucian Jarpug
            Nov 22 at 20:14














          1












          1








          1






          You're dropping the key from Firebase when you're adding the notes to aux. The solution is to also keep the key from Firebase in there:



          notitasRef.on('value', (dataSnapshot) => {
          var aux = ;
          dataSnapshot.forEach((child) => {
          aux.push({
          date: child.val().date,
          notita: child.val().notita,
          id: child.key
          });
          });
          this.setState({all_notitas: aux});
          }


          And then pass that value to deleteNotita with:



          this.deleteNotita(val.id)





          share|improve this answer












          You're dropping the key from Firebase when you're adding the notes to aux. The solution is to also keep the key from Firebase in there:



          notitasRef.on('value', (dataSnapshot) => {
          var aux = ;
          dataSnapshot.forEach((child) => {
          aux.push({
          date: child.val().date,
          notita: child.val().notita,
          id: child.key
          });
          });
          this.setState({all_notitas: aux});
          }


          And then pass that value to deleteNotita with:



          this.deleteNotita(val.id)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 19:46









          Frank van Puffelen

          226k27368395




          226k27368395












          • Just tried it and it worked. I'm feeling dumb, thank you.
            – Lucian Jarpug
            Nov 22 at 20:14


















          • Just tried it and it worked. I'm feeling dumb, thank you.
            – Lucian Jarpug
            Nov 22 at 20:14
















          Just tried it and it worked. I'm feeling dumb, thank you.
          – Lucian Jarpug
          Nov 22 at 20:14




          Just tried it and it worked. I'm feeling dumb, thank you.
          – Lucian Jarpug
          Nov 22 at 20:14


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436746%2fdelete-item-by-key-firebase-react-native%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks