0

I have a really simple JS code:

var worldRef = firebase.database().ref('something/something1');
var something = new Object();
worldRef.on('child_added', function(snap)  {
    something.id = snap.key;
    something.value = snap.val().value;
});
console.log(something);

It works almost fine - just one key&value from database, but it's something. If I modify it:

console.log(something.id);

Undefined. I've tried with map instead of object, but same. I see everything in map, but when I try to call the getmap(key), it's undefined.

Array-based solution:

var worldRef = firebase.database().ref('something/something1');
var something = [];
worldRef.on('child_added', function(snap)  {
  something.push ({
    id: snap.key,
    value: snap.val().value
  })
});
console.log(something);

It works. enter image description here

3
  • What do you exactly mean by "console.log(something); works"? Can you pls show the exact output of this console.log as well as where you call it in your code? Commented Apr 25, 2020 at 14:57
  • Sure. I'm calling it right after the worldRef.on. The output: Object { } After I click on it: id: "US" value: 2373 ​ Commented Apr 25, 2020 at 15:06
  • If I do it with curly braces, it's empty. Inside the function, all data is in my object. Commented Apr 25, 2020 at 15:11

1 Answer 1

1

This is because each time a child is added, you re-assign the id and value properties of your something object.

Since, the child_added event is triggered once for each initial child at this location (see the doc), at the end your object just contains the last children element.

If you do as follows (creating a new property for each child), you will see the same effect than with the Array:

var something = new Object();
worldRef.on('child_added', function (snap) {
   something[snap.key] = snap.val().value;
});
Sign up to request clarification or add additional context in comments.

6 Comments

Please share your entire code. As you have understood the result is highly dependent on the place of each element (in the callback, outside the callback, etc). Please add the code to your initial question, by editing it.
It works, but how should I use the key / value? something.key doesn't work, obviously. :/ So in your solution if I don't know what is the key, I can't use it.
Maybe a simple query is better, then the child_added?
You just have to take into account that the callback function is triggered several times (initially for each initial child at this location and then each time a new child is added). You have to adapt your object structure to follow this constraint.
"Maybe a simple query is better, then the child_added?" It's really difficult to say without knowing your full set of requirements and goal.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.