title: Putting Firestore data into array with Angular
body: <p>I have created a small application for keeping track of how much time I spend in different courses (as a teacher) using Angular 5 and putting my data in Firestore via AngularFire2. Most things have worked out nicely, but for the final part of the application I am having serious problems. I am quite a newbie at Angular/Firebase and most importantly (perhaps) with reactive programming.</p>
  
  <p>The data that is stored in Firestore is quite simple:
  <a href="https://i.stack.imgur.com/KL32K.png" rel="nofollow noreferrer">Data stored in Firebase</a> It consists of six fields for information about the course, when the course was held and most importantly what I did (called Element) and for how long (called Duration).</p>
  
  <p>Now, what I would like to do is to make a report that summaries all the durations for each thing I did (Element). In essence, what I would like to do is the equivalent of a GroupBy in SQL. As I understand it Firebase does not have an operation for grouping.</p>
  
  <p>My, so far failed approach to this, has been to try to create an array into which I put the data and make calculations on that. It is here that I fail.</p>
  
  <p>My first attempt is based on creating an observer as most examples for retrieving data in tutorials use. WorkItem is a simple class that has the same content as in Firebase.</p>
  
  <pre><code>this.lectureDoc = this.afs.collection(`users/${this.afAuth.auth.currentUser.uid}/regTime`,
  ref =&gt; ref.where('course', '==', this.selectedCourse.courseName).orderBy('element') );
  
  this.lectureItems = this.lectureDoc.snapshotChanges().pipe(
    map (courses =&gt; courses.map(a =&gt; {
    const data = a.payload.doc.data() as WorkItem;
    const id = a.payload.doc.id;
    return { id, ...data };
    })
  ));
  
  this.lectureItems.subscribe(item =&gt; { 
    item.forEach(i =&gt; {
      this.allElements.push(i);
    });
  });
  
  console.error(this.allElements);
  console.error(this.allElements[0]);
  </code></pre>
  
  <p>The two last lines show the problem, namely that the first (of the two last lines) will return the complete array while the second will return 'undefined'. </p>
  
  <p>I understand that reactive programming will make asynchronous calls and that I therefore cannot know for sure when the data is filled. However, I do not understand why I can see the content of the array in the first of the two last lines, but not in the second.</p>
  
  <p>My second attempt is based on getting the data from the documents of the collection itself and also, to prevent empty arrays, using .then() but again the two last lines show exactly the same output as previously.</p>
  
  <pre><code>this.lectureDoc.ref.get().then(item =&gt; {
   item.forEach(i =&gt; {
    this.lectures.push(i.data() as WorkItem);
   });
  });
  
  console.error(this.lectures);
  console.error(this.lectures[0]);
  </code></pre>
  
  <p>The output can be seen in this image: <a href="https://i.stack.imgur.com/cmJu4.png" rel="nofollow noreferrer">Output from the running program</a></p>
  
  <p>So, to recap, what I would like to do is to collect all the data in an array of WorkItem and then calculate how much time has been spent on different tasks and then display it in list on the web page. I have not come to the display part and I suspect that I will have trouble again with the array not being populated when bound to the list. I have a hard time to understand reactive programming...</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>//Tobias</p>