1
を使用して、firebase基準値を更新することができません: 1.製品ユーザーは選択肢のリストを完了したときに私はfirebaseで2つのリファレンス持って約束=> promise.all
シナリオがある 2.栄養素、彼らの栄養値を(1)
ここでは、私が作業しようとしているコードがありますが、私が紛失しているかわからない、以下のコードは最初の製品の栄養成分のみをアップデートしています。 。私はAngular2とfirebaseで新しいです。
//capturing promises
let promises = Object.keys(choices)
.map(k => {
return new Promise((resolve, reject) => {
this.updateNutrtion(choices[k].choice,resolve,reject);
});
});
//promise.all
Promise.all(promises).then((object)=> console.log(object));
updateNutrtion(choice,res,rej){
//logic to get the nutritions
return new Promise((res,rej)=>{
firebase.database().ref('/products/' + choice).once('value',function(snapshot){
snapshot.forEach(function(child){
var consumeDay;
var today = new Date().toISOString();
var year = today.split("-")[0];
var month = today.split("-")[1];
var day = (today.split("-")[2]).split("T")[0]
consumeDay = day + '-' + month + '-' + year;
// update firebase nutrition
firebase.database().ref('/nutritions/' + firebase.auth().currentUser.uid + '/' + consumeDay + '/' + child.ref.key).once('value',function(nutrition){
firebase.database().ref('/nutritions/' + firebase.auth().currentUser.uid + '/' + consumeDay + '/' + child.ref.key).update({
unit : child.val().unit,
value : (nutrition.val()!=null) ? (nutrition.val().value + child.val().value) : (child.val().value)
})
})
return false;
})
}).then((key)=>{
res(key);
});
})
}
私が間違っているところを助けてください。
まだこれで苦労しています。どんな援助も。 – chits