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);
      });
    });
  }
});
}
PR7
  • 1,524
  • 1
  • 13
  • 24
  • 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. –  Jun 18 '17 at 20:02
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –  Jun 18 '17 at 20:09

3 Answers3

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.

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))

Ced
  • 15,847
  • 14
  • 87
  • 146
  • `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 .... – PR7 Jun 18 '17 at 20:06
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.

Garamaru
  • 106
  • 1
  • 1
  • 11