1

I'm running into a weird issue where once I push a value to a Firebase object, two things happen:

  1. I can't access it in the array I receive when I pull the object from Firebase.
  2. The array I receive has no length property.

Here is what my Firebase structure looks like:

"user_solutions": { "0": { "user_id": 0, "clue_id": 0, "hunt_id": 0, "completed": 1 }, "1": { "user_id": 0, "clue_id": 1, "hunt_id": 0, "completed": 0 }, "2": { "user_id": 0, "clue_id": 1, "hunt_id": 1, "completed": 0 } }, "-KHxBMZwVMzyiMIcbMdr": { clue_id: 1, completed: 0, hunt_id: 0, user_id: 0 } 

Here is the problematic function:

userSolutionsRef.orderByChild('user_id').startAt(0).endAt(0).once('value', (snap) => { var solution = snap.val(); for (var i = 0; i < solution.length; i++) { if (solution[i].hunt_id == 0) { solutionsForThisHunt.push(solution[i]); } } this.populateArray(solutionsForThisHunt); }); 

When I run the debugger, solution has the value Object {0: Object, 1: Object, 2: Object, -KHxBMZwVMzyiMIcbMdr: Object}, but the length property is undefined. solutionsForThisHunt never gets populated. Also, I can access the objects individually in the debugger via snap.val()[0]/snap.val()[1]/snap.val()[2], but for some reason when I try accessing snap.val()[3] it is undefined, although it should be populated.

3

1 Answer 1

3

solution seems to be an object with keys, rather than an array. You could do the following

var solution = snap.val(); var array = Object.keys(solution).map(key => ({ ...solution[key], id: key })); for .... 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.