Firestore documentReference not initializing quickly enough











up vote
0
down vote

favorite












I'm trying to create a firebase application, but something happens whenever I refresh the page.



list book page



PLACEHOLDER is an ion-item filled with data retrieved from a firebase.firestore.CollectionReference bookListRef that is maintained within a service known as BooksService, and its method the page calls is getBookEntries(bookId).



import { Injectable } from '@angular/core';
import * as firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';
import 'firebase/storage';

@Injectable({
providedIn: 'root'
})
export class BooksService {
public bookListRef: firebase.firestore.CollectionReference;
constructor() {
firebase.auth().onAuthStateChanged(user => {
if (user) {
this.bookListRef = firebase.firestore().collection(`/bookList`);
console.log(this.bookListRef);
}
});
}

addBook(authorName: string, bookTitle: string, edition: string): Promise<firebase.firestore.DocumentReference>{
return this.bookListRef.add({
authorLastName: authorName,
title: bookTitle,
edition: edition
}).then((bookRef)=>
bookRef.collection("Entries").add({
page: "PLACEHOLDER",
})

);


}

getBookList(authorLastName): firebase.firestore.Query{
return this.bookListRef.where("authorLastName", "==", authorLastName);
}

getBookEntries(bookId: string): firebase.firestore.CollectionReference{

return this.bookListRef.doc(bookId).collection("Entries");
}
}
}


When I first load the page, the PLACEHOLDER data is loaded just fine. But when I refresh the page, the bookListRef on which getBookEntries(bookId) performs the query is undefined. By inserting console.log statements throughout the code, I found that getBookEntries method would execute before the constructor had finished initializing the bookListRef. What's really puzzling is that I'm following Javabratt's firestore tutorial EventManager section, and this behaviour doesn't come up. I can't find any fundamental differences in the way our respective services are written, nor in how our pages call those services. I added console.log statements to his example, and somehow his constructor is completing the firebase query and initializing eventListRef before the getEventDetail() has to operate on it.



The rest of the post details my efforts to make this work, and is skippable.
Thank you for reading.



I've been trying to find a way to ensure that getBookEntries(bookId) doesn't start executing until after the constructor finishes. I tried using a while loop to stall the getBookEntries(bookId) until initialization had happened, but control never left the loop and I was left with a freezing memory hog. I looked into await/async but that can't be used on a constructor, and ngOnInit can't be used in services.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm trying to create a firebase application, but something happens whenever I refresh the page.



    list book page



    PLACEHOLDER is an ion-item filled with data retrieved from a firebase.firestore.CollectionReference bookListRef that is maintained within a service known as BooksService, and its method the page calls is getBookEntries(bookId).



    import { Injectable } from '@angular/core';
    import * as firebase from 'firebase/app';
    import 'firebase/auth';
    import 'firebase/firestore';
    import 'firebase/storage';

    @Injectable({
    providedIn: 'root'
    })
    export class BooksService {
    public bookListRef: firebase.firestore.CollectionReference;
    constructor() {
    firebase.auth().onAuthStateChanged(user => {
    if (user) {
    this.bookListRef = firebase.firestore().collection(`/bookList`);
    console.log(this.bookListRef);
    }
    });
    }

    addBook(authorName: string, bookTitle: string, edition: string): Promise<firebase.firestore.DocumentReference>{
    return this.bookListRef.add({
    authorLastName: authorName,
    title: bookTitle,
    edition: edition
    }).then((bookRef)=>
    bookRef.collection("Entries").add({
    page: "PLACEHOLDER",
    })

    );


    }

    getBookList(authorLastName): firebase.firestore.Query{
    return this.bookListRef.where("authorLastName", "==", authorLastName);
    }

    getBookEntries(bookId: string): firebase.firestore.CollectionReference{

    return this.bookListRef.doc(bookId).collection("Entries");
    }
    }
    }


    When I first load the page, the PLACEHOLDER data is loaded just fine. But when I refresh the page, the bookListRef on which getBookEntries(bookId) performs the query is undefined. By inserting console.log statements throughout the code, I found that getBookEntries method would execute before the constructor had finished initializing the bookListRef. What's really puzzling is that I'm following Javabratt's firestore tutorial EventManager section, and this behaviour doesn't come up. I can't find any fundamental differences in the way our respective services are written, nor in how our pages call those services. I added console.log statements to his example, and somehow his constructor is completing the firebase query and initializing eventListRef before the getEventDetail() has to operate on it.



    The rest of the post details my efforts to make this work, and is skippable.
    Thank you for reading.



    I've been trying to find a way to ensure that getBookEntries(bookId) doesn't start executing until after the constructor finishes. I tried using a while loop to stall the getBookEntries(bookId) until initialization had happened, but control never left the loop and I was left with a freezing memory hog. I looked into await/async but that can't be used on a constructor, and ngOnInit can't be used in services.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to create a firebase application, but something happens whenever I refresh the page.



      list book page



      PLACEHOLDER is an ion-item filled with data retrieved from a firebase.firestore.CollectionReference bookListRef that is maintained within a service known as BooksService, and its method the page calls is getBookEntries(bookId).



      import { Injectable } from '@angular/core';
      import * as firebase from 'firebase/app';
      import 'firebase/auth';
      import 'firebase/firestore';
      import 'firebase/storage';

      @Injectable({
      providedIn: 'root'
      })
      export class BooksService {
      public bookListRef: firebase.firestore.CollectionReference;
      constructor() {
      firebase.auth().onAuthStateChanged(user => {
      if (user) {
      this.bookListRef = firebase.firestore().collection(`/bookList`);
      console.log(this.bookListRef);
      }
      });
      }

      addBook(authorName: string, bookTitle: string, edition: string): Promise<firebase.firestore.DocumentReference>{
      return this.bookListRef.add({
      authorLastName: authorName,
      title: bookTitle,
      edition: edition
      }).then((bookRef)=>
      bookRef.collection("Entries").add({
      page: "PLACEHOLDER",
      })

      );


      }

      getBookList(authorLastName): firebase.firestore.Query{
      return this.bookListRef.where("authorLastName", "==", authorLastName);
      }

      getBookEntries(bookId: string): firebase.firestore.CollectionReference{

      return this.bookListRef.doc(bookId).collection("Entries");
      }
      }
      }


      When I first load the page, the PLACEHOLDER data is loaded just fine. But when I refresh the page, the bookListRef on which getBookEntries(bookId) performs the query is undefined. By inserting console.log statements throughout the code, I found that getBookEntries method would execute before the constructor had finished initializing the bookListRef. What's really puzzling is that I'm following Javabratt's firestore tutorial EventManager section, and this behaviour doesn't come up. I can't find any fundamental differences in the way our respective services are written, nor in how our pages call those services. I added console.log statements to his example, and somehow his constructor is completing the firebase query and initializing eventListRef before the getEventDetail() has to operate on it.



      The rest of the post details my efforts to make this work, and is skippable.
      Thank you for reading.



      I've been trying to find a way to ensure that getBookEntries(bookId) doesn't start executing until after the constructor finishes. I tried using a while loop to stall the getBookEntries(bookId) until initialization had happened, but control never left the loop and I was left with a freezing memory hog. I looked into await/async but that can't be used on a constructor, and ngOnInit can't be used in services.










      share|improve this question















      I'm trying to create a firebase application, but something happens whenever I refresh the page.



      list book page



      PLACEHOLDER is an ion-item filled with data retrieved from a firebase.firestore.CollectionReference bookListRef that is maintained within a service known as BooksService, and its method the page calls is getBookEntries(bookId).



      import { Injectable } from '@angular/core';
      import * as firebase from 'firebase/app';
      import 'firebase/auth';
      import 'firebase/firestore';
      import 'firebase/storage';

      @Injectable({
      providedIn: 'root'
      })
      export class BooksService {
      public bookListRef: firebase.firestore.CollectionReference;
      constructor() {
      firebase.auth().onAuthStateChanged(user => {
      if (user) {
      this.bookListRef = firebase.firestore().collection(`/bookList`);
      console.log(this.bookListRef);
      }
      });
      }

      addBook(authorName: string, bookTitle: string, edition: string): Promise<firebase.firestore.DocumentReference>{
      return this.bookListRef.add({
      authorLastName: authorName,
      title: bookTitle,
      edition: edition
      }).then((bookRef)=>
      bookRef.collection("Entries").add({
      page: "PLACEHOLDER",
      })

      );


      }

      getBookList(authorLastName): firebase.firestore.Query{
      return this.bookListRef.where("authorLastName", "==", authorLastName);
      }

      getBookEntries(bookId: string): firebase.firestore.CollectionReference{

      return this.bookListRef.doc(bookId).collection("Entries");
      }
      }
      }


      When I first load the page, the PLACEHOLDER data is loaded just fine. But when I refresh the page, the bookListRef on which getBookEntries(bookId) performs the query is undefined. By inserting console.log statements throughout the code, I found that getBookEntries method would execute before the constructor had finished initializing the bookListRef. What's really puzzling is that I'm following Javabratt's firestore tutorial EventManager section, and this behaviour doesn't come up. I can't find any fundamental differences in the way our respective services are written, nor in how our pages call those services. I added console.log statements to his example, and somehow his constructor is completing the firebase query and initializing eventListRef before the getEventDetail() has to operate on it.



      The rest of the post details my efforts to make this work, and is skippable.
      Thank you for reading.



      I've been trying to find a way to ensure that getBookEntries(bookId) doesn't start executing until after the constructor finishes. I tried using a while loop to stall the getBookEntries(bookId) until initialization had happened, but control never left the loop and I was left with a freezing memory hog. I looked into await/async but that can't be used on a constructor, and ngOnInit can't be used in services.







      javascript firebase ionic-framework google-cloud-firestore






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Frank van Puffelen

      220k25361387




      220k25361387










      asked yesterday









      Joel Nash

      195




      195





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417011%2ffirestore-documentreference-not-initializing-quickly-enough%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417011%2ffirestore-documentreference-not-initializing-quickly-enough%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Catalogne

          Violoncelliste

          Héron pourpré