I'm having a problem on updating a value in firebase. Here is the scenario and code: I'm running a forEach loop on some data, about 300 records. I update state locally and in the callback I have a function that executes this code (partial).
.then(memKey => {
this.setState({
memberKeyID: memKey,
family: {[memKey]: [first, last, 'Head of house']},
memberSaved: true,
readOnly: true
}, () => {
this.props.firebase.getMemberCount(currentOrganization).once('value')
.then(count => {
console.log(count.val());
if(count.val()) {
const countTotal = count.val() + 1;
return countTotal;
} else {
return 1;
}
})
.then(memberCount => {
console.log(memberCount);
this.props.firebase.setMemberCount(currentOrganization, memberCount)
})
})
})
I make a call to getMemberCount
which at the time returns 5
when we console.log(count.val()) I get '5'. So going off this I would expect when I call the next then and pass memberCount that I would set an updated value.
In example:
First time it runs countTotal = 5 + 1; should return 6
then next time countTotal = 6 + 1 etc.
However that is not the case. I'm getting 5 for the amount of records we loop.
When data has 300 records I get '5' console logged 300 times.
What am I missing here?