So I'm having a very hard time figuring out how to create a function that will work for different firebase queries / different promises.
Consider this promise, that will turn an empty output array to the returned data of the query / promise.
//userKeys is an array with my 'queries'
Promise.all(userKeys.map(function(key) {
return database.child(key).once("value") //Returns a promise
})).then(function(respond) {
userData = respond;
});
This works perfectly. So I thought I would create a function that will do just that for different queries, and thats where I just reached a dead end.
So I will just wrap my previous code in a function
function query(keys, array) {
return Promise.all(keys.map(function(key) {
return ref.child(key).once("value")
})).then(function(respond) {
array = respond; //This won't work
return respond;
});
}
So I tried returning the promise which I can then do this on
someQuery = query(queryKeys);
someQuery.then(function(data){ //Manual written array to store data here
queryResults = data})
I still need to store that data in an array programmatically, which I just cannot figure out.
So look back at my second snippet of code above (My function) The second parameter was meant to be an array that I want to pass, an array of my choice that will be filled with the data, but that won't work either. If I simplify my function even more I start seeing strange things.
output = [];
function modify(arrayName) {
var data = ["1", "2", "3", "4"];
arrayName = data; //Doesnt work
arrayName = data.slice() //Doesnt work
arrayName.push(data) //Works, but I don't want to push I want to copy
}
modify(output); //Output will stay empty
Why does my output array stay empty? Is this normal? Also when I console log arrayName it returns an object, when its clearly an array. Can someone please elaborate ?
I have a feeling I will eventually have to write a promise manually for every query I will be making in the future, I just wonder if theres a more efficient way of doing that.