【发布时间】:2019-10-11 05:10:38
【问题描述】:
我对此感到非常困惑。我知道这与异步性质有关,但我无法让它按照我需要的方式运行。
我需要 firebase.set 运行。完成后 this.setstate 运行,一旦完成下一个运行。
.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'
})
});
})
目前 console.log(2) 在 console.log(1) 之前运行。我希望 this.setstate 在它移动到下一个 .then 之前解决
如何做到这一点?
【问题讨论】:
标签: reactjs