Ad

Ionic Firebase Once Snapshot Get Weird Bugs

I'm trying to set something through firebase realtime database using set code but before that, I need to use once snapshot to get value and update it.

When I have an array that contains strings ex.) array[0] = "aaa"; array[1] = "bbb"; I should be able to get each string but I'm getting only the last element of array.

First of all, the array contained an object like { name:"name", num:2, isEmpty:false } so I tried to fix it to contain only string using array.push("string"); but I still get same bug.

Before using once('value',snapshot){...} I had no problem with getting element from array but when I use once() I only get last element of array with invalid index.

array[0] = 'aaaa'; array[1] ='bbbb';

for(var i=0;i<array.length;i++){
  alert(array[i]+' '+i); 
  //this works fine, alerts aaaa 0, bbbb 1   

  firebase.database().ref('/Something/').child(array[i]).child('size').once('value',snap=>{
     alert(array[i]+' '+i); 
     //alerts bbbb 2, bbbb 2, I really don't get the issue, snap.val() works fine tho but only get bbbb's snap.val()
  });
}

This is sample code, I tried to set something inside of once(), but it only sets for 'bbbb' path's child. It never goes on 'aaaa' path.

it must alert aaaa 0, bbbb 1. but why am I getting bbbb 2, bbbb 2? the index is even invalid the length of the array is 2. I really have no idea about this issue. Please can someone help me out of this one?

*I tried everything I could, changing array,using on() instead of once(), using then() after once(), etc.. but by the sample code, it must not have any issues right???

Ad

Answer

I fixed this problem with 'async' function, anyone who has same issues with me, just make another async function to do something in once()

Ad
source: stackoverflow.com
Ad