I'm very confused on this. I know it has to do with the async nature but I'm not able to get this to function the way I need it to.
I need the firebase.set to run. When that is completed then this.setstate to run, once that is completed the next then to run.
.then(keyUid => {
let updatedFamily = {...family, [keyUid]: [firstHousehold, lastHousehold, householdDropdown]};
this.props.firebase.db.ref('organization/' + currentOrganization + '/members/' + keyUid ).set(
{
first: firstHousehold,
last: lastHousehold,
phone: phone,
email: email,
address: address,
address2: address2,
city: city,
zip: zip,
headOfHousehold: headOfHousehold,
}, (error) => {
if(error){
console.log(error);
}
else{
this.setState({ family: updatedFamily }, () => console.log(1));
}
})
})
.then(newHouseholdUid => {
console.log(2);
Object.keys(family).forEach(key => {
this.props.firebase.db.ref('organization/' + currentOrganization + '/members/' + key ).update({
family: 'howdly'
})
});
})
Currently console.log(2) runs BEFORE console.log(1). I would expect for this.setstate to resolve before it moves on to the next .then
How can achieve this?