I have this code:
setProductsToUser: function(productsArr, userId) {
console.log('***********************************************');
channelsToActivate = {}
count = 0
for (var i = 0; i < productsArr.length; i++) {
console.log('ref.child(user_products).child('+userId+').child('+productsArr[i]+').set(true);')
count++
ref.child('products').child(productsArr[i]).once('value', function(productSnap) {
count--
console.log(count)
if(productSnap.val()){
for (key in productSnap.val().channels) {
channelsToActivate[key] = true
console.log(channelsToActivate)
}
if (count == 0){
for (key in channelsToActivate) {
console.log('ref.child(system_user).child('+key+').child('+userId+').set(true);')
console.log('ref.child(user_system).child('+userId+').child('+key+').set(true);')
}
}
}
})
}
And I sent code like this:
productsM.setProductsToUser(['com-dummy-dummy-1month'], '2HWdYcvGydTYdrxn2ZGCFsGYV8J2')
productsM.setProductsToUser(['com-dummy-dummy-3months'], '2Wv6tZWC4EO0cb2JivqXLu53S4q1')
The output is:
***********************************************
ref.child(user_products).child(2HWdYcvGydTYdrxn2ZGCFsGYV8J2).child(com-dummy-dummy-1month).set(true);
***********************************************
ref.child(user_products).child(2Wv6tZWC4EO0cb2JivqXLu53S4q1).child(com-dummy-dummy-3months).set(true);
0
{ '-KUrkIm_pxcACN_ONuWd': true }
-KUrkIm_pxcACN_ONuWd
ref.child(system_user).child(-KUrkIm_pxcACN_ONuWd).child(2HWdYcvGydTYdrxn2ZGCFsGYV8J2).set(true);
ref.child(user_system).child(2HWdYcvGydTYdrxn2ZGCFsGYV8J2).child(-KUrkIm_pxcACN_ONuWd).set(true);
-1
{ '-KUrkIm_pxcACN_ONuWd': true }
It should be:
***********************************************
ref.child(user_products).child(2HWdYcvGydTYdrxn2ZGCFsGYV8J2).child(com-dummy-dummy-1month).set(true);
0
{ '-KUrkIm_pxcACN_ONuWd': true }
-KUrkIm_pxcACN_ONuWd
ref.child(system_user).child(-KUrkIm_pxcACN_ONuWd).child(2HWdYcvGydTYdrxn2ZGCFsGYV8J2).set(true);
ref.child(user_system).child(2HWdYcvGydTYdrxn2ZGCFsGYV8J2).child(-KUrkIm_pxcACN_ONuWd).set(true);
***********************************************
ref.child(user_products).child(2Wv6tZWC4EO0cb2JivqXLu53S4q1).child(com-dummy-dummy-3months).set(true);
0
{ '-KUrkIm_pxcACN_ONuWd': true }
-KUrkIm_pxcACN_ONuWd
ref.child(system_user).child(-KUrkIm_pxcACN_ONuWd).child(2Wv6tZWC4EO0cb2JivqXLu53S4q1).set(true);
ref.child(user_system).child(2Wv6tZWC4EO0cb2JivqXLu53S4q1).child(-KUrkIm_pxcACN_ONuWd).set(true);
I get succesfully the last output placing a setTimeout with 4 seconds something like this:
productsM.setProductsToUser(['com-dummy-dummy-1month'], '2HWdYcvGydTYdrxn2ZGCFsGYV8J2')
setTimeout(function() {
productsM.setProductsToUser(['com-dummy-dummy-3months'], '2Wv6tZWC4EO0cb2JivqXLu53S4q1')
}, 4000);
How can I reach the desired output without a timeout in every call? (I will loop like one hundred times for that function in my code - setProductsToUser) Why the counter goes to -n ?
via arnoldssss
No comments:
Post a Comment