0

I have a subscribed to a FirebaseObjectObservable to get value of a property of an object from my firebase database. This property can contain multiple values so I stored the values in a local array variable.

But I am unable to iterate through this local array variable using forEach. So I console.log this array variable. This variable seems to have json format maybe thats why I am unable to iterate. Please help me find a way to iterate through this variable.

* Code and Console Images :

Code Screenshot

Console Screenshot

* Edit *

I shifted the code inside the subscribe method to iterate.

getAttachments() {
this.assignment.subscribe(asnDetails => {
  if (asnDetails.fileKey) {
    this.fileKeys.push(asnDetails.fileKey);
    this.fileKeys[0].forEach(asnFileKey => {  // <---- shifted this forEach loop inside subscribe 
      this.asnFiles = this._getAsnService.getAssignmentFiles(asnFileKey);
      this.asnFiles.subscribe(file => {
        this.fileNames.push(file.name);
      });
    });
  }
});
}
2
  • Please put the relevant code in the question. Also, please write in correct international English, in which the word "I" is capitalized. Finally, please spare us unnecessary details about where you are in your learning curve, or how grateful you are for future answers. Commented Jun 18, 2017 at 20:02
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Jun 18, 2017 at 20:09

3 Answers 3

2

The this.fileKeys value is available only within the subscribe handler. That's basic to asynchronous programming. Your console log is immediately after the subscribe, and is executed before the subscription has had a change to fire.

Sign up to request clarification or add additional context in comments.

Comments

1

You have one nested array in your screenshot. The first one is of length 1 and the nested one is of length two. So you can access array[0].forEach(item => console.log(item))

1 Comment

this.fileKeys[0].forEach( item => { console.log(item); }); tried this just now, got this error : Uncaught (in promise): TypeError: Cannot read property 'forEach' of undefined TypeError: Cannot read property 'forEach' of undefined at ....
0

Sadly, I can't comment. It's not an angular or firebase problem.
It's a normal array, based on your console screenshot.
Have you tried to iterate through this.fileKeys?
If someone could post this as a comment, I would delete this as an answer.

4 Comments

yes i tried iterating this.fileKeys but it seems like its not entering the loop. this.fileKeys.forEach( x => { console.log("Iterating..."); });
Can you do a console.log(this.fileKeys.length)) please and post the result?
console.log(this.fileKeys.length) returns 0
and 1 if used inside the subscribe

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.