Randy
Randy

Reputation: 105

React-native button press performs 2 out of 3 Firebase database updates

When this button is pressed, it needs to run three database updates. So far it does two of those updates. This is what it produces (in black), and what I'm trying to achieve (in blue). Please show how to get all three updates to work. I think that maybe the last update, which has a return statement, is running before the failed update, and the return statement ends the whole function.

results

<Button
        title='Up'
        color='green'
        onPress={() => recordUpVote(this.state.currentUser, 'AMZN')}
      />

function recordUpVote(user, instr){
  var today = moment().format('MMDDYYYY');

firebase.database().ref('/votes/' +today+ '/' +instr+ '/voters').push({
  user: user.id  // THIS IS NOT EXECUTING... or is being overwritten..?
    }).then(()=>{
     firebase.database().ref('/users/'+user.uid+ '/votes/' + today+ '/').push({
       instrument: instr,
       vote: 'up'
 }).then(()=>{
       firebase.database().ref('/votes/' +today+ '/' +instr+ '/upVotes').transaction(function(upVotes) {
         return upVotes + 1
       });
     });
});

} // end recordUpVote

Upvotes: 0

Views: 169

Answers (2)

Randy
Randy

Reputation: 105

This is just dumb, but I was calling user.id instead of user.uid.

function recordUpVote(user, instr){
  var today = moment().format('MMDDYYYY');
  firebase.database().ref('/votes/' +today+ '/' +instr+ '/voters/').push({
    voters: user.uid
      }).then(()=>{
       firebase.database().ref('/users/'+user.uid+ '/votes/' + today+ '/').push({
         instrument: instr,
         vote: 'up'
       }).then(()=>{
         firebase.database().ref('/votes/' +today+ '/' +instr+ '/upVotes').transaction(function(upVotes) {
           return upVotes + 1
         });
       });
  });
} // end recordUpVote

Upvotes: 0

Praz Solver
Praz Solver

Reputation: 543

Use Nested push with then promise to nest one update after another.

firebase.database().ref('.....').push().then(()=>{

     firebase.database().ref('.....').push().then(()=>{

        firebase.database().ref('.....').push().then(()=>{

         });

     });

});

Upvotes: 1

Related Questions