Ionic4 / Firebase Real time db / javascript / snapshot / how to get data for real


I start using Firebase real time db and I’d like to know how to extract data quick and easy.

Here is my code:

this.MyFormula = ref.once(“value”).then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
childData.key = childSnapshot.key;
console.log(“This display works well:”,childData);
return returnArr;

The display inside the Snapshot works well but impossible to access the array “childData” else error from Angular (note: there is an error raised in Visual Studio Code for my field “this.MyFormula” but it works anyway).

If I display this.MyFormula I get:

But how to extract my useful data in this? (this might be a silly question but I don’t know if it is Javascript or Typescript or Firebase related).


You can’t.

Asynchronously delivered results are only usable in the code block they’re delivered to. Look through these search results for many stories of people wondering similar things.

Listen to that error, because it isn’t “working anyway”. Presumably the error is telling you that you can’t treat a Promise interchangeably with its resolution, and it’s right: you can’t, and you shouldn’t be assigning Promises to object properties anyway unless you’re completely certain of what it means to be doing so.

Also, never type the word function inside of one.

Structure your code so that the first word of any method dealing with futures is return, and then use either then or subscribe (depending on whether you’re choosing to return a Promise or Observable) in the ultimate caller, inside which the data will be delivered when it’s ready.