我在firebase中有两个引用: 产品 2.营养
情景是当用户填写一份选择清单时,他们在营养品中的营养价值(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);
});
})
}
请在我出错的地方提供帮助。
答案 0 :(得分:0)
解决了它。如果这有助于其他人,请在下面发布代码。
//handelling promises over an array
let promises = Object.keys(choices)
.map(k => {
var val =this.updateAllNutritions(choices[k].choice)
.then(function(values) {
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;
// console.log('all done', values); // [snap, snap, snap]
Object.keys(values)
.map(j=>{
var unit = firebase.database().ref('/nutritions/' + firebase.auth().currentUser.uid + '/' + consumeDay + '/' + values[j].valueOf).child('unit')
var value = firebase.database().ref('/nutritions/' + firebase.auth().currentUser.uid + '/' + consumeDay + '/' + values[j].valueOf).child('value')
unit.transaction(function(currentUnit){
return values[j].unit;
});
value.transaction(function(currentValue){
return currentValue + values[j].value;
})
})
});
});
//update nutritions async function
updateAllNutritions(choice){
return firebase.database().ref('/products/' + choice).once('value').then(function(snapshot){
var nutritions = [];
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;
snapshot.forEach(function(childSnapshot){
var promise = firebase.database().ref('/nutritions/' + firebase.auth().currentUser.uid + '/' + consumeDay + '/' + childSnapshot.ref.key).once('value').then(function(snap){
var snapNull = null;
if(snap.val()!=null){
// console.log("currentvalue" + snap.val().value);
snapNull = snap.val().value
}
let promisedettrue = {
unit : childSnapshot.val().unit,
value : childSnapshot.val().value,
valueOf : childSnapshot.ref.key,
isNodenull : false,
currentValue : snapNull
};
let promisedetfalse = {
unit : childSnapshot.val().unit,
value : childSnapshot.val().value,
valueOf : childSnapshot.ref.key,
isNodenull : true,
};
if(snap.val()==null){
return promisedetfalse;
}else{
return promisedettrue;
}
},function(error){
// The Promise was rejected.
console.error(error);
});
nutritions.push(promise);
});
return Promise.all(nutritions);
},function(error){
// The Promise was rejected.
console.error(error);
})
}