Calling then function after fetching data from firebase in ionic 3











up vote
1
down vote

favorite












I want fetch data from firebase after that I want to execute another function. Second function have to wait until first one is complete .



this.oAngularFireDatabase.database.ref('Users').orderByKey()
.on('value', snapshot => {
if (snapshot.hasChildren()) {
snapshot.forEach(innerSnap => {
if (innerSnap.hasChild(user.uid)) {
//User role key
this.loggedInUserUserRoleKey = innerSnap.key;
//User id
this.loggedInUserId = user.uid;

//User name
this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

if (innerSnap.child(user.uid).hasChild("user_image")) {
//User Image
this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
}
return false;
}
})
}
})


I can't call then function after on it gives me an error.
In my above code, I want call another function after all data are fetch from firebase.










share|improve this question




























    up vote
    1
    down vote

    favorite












    I want fetch data from firebase after that I want to execute another function. Second function have to wait until first one is complete .



    this.oAngularFireDatabase.database.ref('Users').orderByKey()
    .on('value', snapshot => {
    if (snapshot.hasChildren()) {
    snapshot.forEach(innerSnap => {
    if (innerSnap.hasChild(user.uid)) {
    //User role key
    this.loggedInUserUserRoleKey = innerSnap.key;
    //User id
    this.loggedInUserId = user.uid;

    //User name
    this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

    if (innerSnap.child(user.uid).hasChild("user_image")) {
    //User Image
    this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
    }
    return false;
    }
    })
    }
    })


    I can't call then function after on it gives me an error.
    In my above code, I want call another function after all data are fetch from firebase.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want fetch data from firebase after that I want to execute another function. Second function have to wait until first one is complete .



      this.oAngularFireDatabase.database.ref('Users').orderByKey()
      .on('value', snapshot => {
      if (snapshot.hasChildren()) {
      snapshot.forEach(innerSnap => {
      if (innerSnap.hasChild(user.uid)) {
      //User role key
      this.loggedInUserUserRoleKey = innerSnap.key;
      //User id
      this.loggedInUserId = user.uid;

      //User name
      this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

      if (innerSnap.child(user.uid).hasChild("user_image")) {
      //User Image
      this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
      }
      return false;
      }
      })
      }
      })


      I can't call then function after on it gives me an error.
      In my above code, I want call another function after all data are fetch from firebase.










      share|improve this question















      I want fetch data from firebase after that I want to execute another function. Second function have to wait until first one is complete .



      this.oAngularFireDatabase.database.ref('Users').orderByKey()
      .on('value', snapshot => {
      if (snapshot.hasChildren()) {
      snapshot.forEach(innerSnap => {
      if (innerSnap.hasChild(user.uid)) {
      //User role key
      this.loggedInUserUserRoleKey = innerSnap.key;
      //User id
      this.loggedInUserId = user.uid;

      //User name
      this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

      if (innerSnap.child(user.uid).hasChild("user_image")) {
      //User Image
      this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
      }
      return false;
      }
      })
      }
      })


      I can't call then function after on it gives me an error.
      In my above code, I want call another function after all data are fetch from firebase.







      javascript firebase ionic-framework firebase-realtime-database angularfire2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 14:36









      Frank van Puffelen

      218k25361386




      218k25361386










      asked Nov 8 at 11:05









      ThusharaHettigoda

      539




      539
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The Firebase on() method can fire multiple times: once when it initially loads the data, and again whenever the data changes. Since a promise (the thing you call then() on) can only resolve once, on() can't return a promise.



          There are two options here:





          1. You want to only load the data once.



            If this is the case, you should use Firebase's once() method, which does return a promise.



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .once('value').then(snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            return false;
            }
            })
            }
            }).then(value => {
            // TODO: perform subsequent action on boolean value
            })



          2. You want to listen for changes on the data too.



            If this is the case, you should put the subsequent action you want to take into the on() callback:



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .on('value', snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            }
            })
            // TODO: perform subsequent action on data
            }
            })



          Note that both of these operations look pretty expensive for what they're trying to accomplish: scanning a JSON tree for a specific value is an anti-pattern in Firebase, and typically means you should modify/augment your JSON to allow a direct lookup or query.



          For example, I suspect you now have a structure like /Users/$randomkey/$uid: { ..user data... }. For better performance, consider storing the user data directly under their UID: /Users/$uid: { ..user data... }. This removes the need for a query, and allows you to directly load the data for a user from this.oAngularFireDatabase.database.ref('Users').child(user.uid).






          share|improve this answer





















          • you saved my time brother @Frank van Puffelen
            – ThusharaHettigoda
            Nov 9 at 5:03












          • i have structure like Users/$UserRoleKey/$uid:{}
            – ThusharaHettigoda
            Nov 9 at 5:07











          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%2f53206459%2fcalling-then-function-after-fetching-data-from-firebase-in-ionic-3%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          The Firebase on() method can fire multiple times: once when it initially loads the data, and again whenever the data changes. Since a promise (the thing you call then() on) can only resolve once, on() can't return a promise.



          There are two options here:





          1. You want to only load the data once.



            If this is the case, you should use Firebase's once() method, which does return a promise.



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .once('value').then(snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            return false;
            }
            })
            }
            }).then(value => {
            // TODO: perform subsequent action on boolean value
            })



          2. You want to listen for changes on the data too.



            If this is the case, you should put the subsequent action you want to take into the on() callback:



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .on('value', snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            }
            })
            // TODO: perform subsequent action on data
            }
            })



          Note that both of these operations look pretty expensive for what they're trying to accomplish: scanning a JSON tree for a specific value is an anti-pattern in Firebase, and typically means you should modify/augment your JSON to allow a direct lookup or query.



          For example, I suspect you now have a structure like /Users/$randomkey/$uid: { ..user data... }. For better performance, consider storing the user data directly under their UID: /Users/$uid: { ..user data... }. This removes the need for a query, and allows you to directly load the data for a user from this.oAngularFireDatabase.database.ref('Users').child(user.uid).






          share|improve this answer





















          • you saved my time brother @Frank van Puffelen
            – ThusharaHettigoda
            Nov 9 at 5:03












          • i have structure like Users/$UserRoleKey/$uid:{}
            – ThusharaHettigoda
            Nov 9 at 5:07















          up vote
          1
          down vote



          accepted










          The Firebase on() method can fire multiple times: once when it initially loads the data, and again whenever the data changes. Since a promise (the thing you call then() on) can only resolve once, on() can't return a promise.



          There are two options here:





          1. You want to only load the data once.



            If this is the case, you should use Firebase's once() method, which does return a promise.



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .once('value').then(snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            return false;
            }
            })
            }
            }).then(value => {
            // TODO: perform subsequent action on boolean value
            })



          2. You want to listen for changes on the data too.



            If this is the case, you should put the subsequent action you want to take into the on() callback:



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .on('value', snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            }
            })
            // TODO: perform subsequent action on data
            }
            })



          Note that both of these operations look pretty expensive for what they're trying to accomplish: scanning a JSON tree for a specific value is an anti-pattern in Firebase, and typically means you should modify/augment your JSON to allow a direct lookup or query.



          For example, I suspect you now have a structure like /Users/$randomkey/$uid: { ..user data... }. For better performance, consider storing the user data directly under their UID: /Users/$uid: { ..user data... }. This removes the need for a query, and allows you to directly load the data for a user from this.oAngularFireDatabase.database.ref('Users').child(user.uid).






          share|improve this answer





















          • you saved my time brother @Frank van Puffelen
            – ThusharaHettigoda
            Nov 9 at 5:03












          • i have structure like Users/$UserRoleKey/$uid:{}
            – ThusharaHettigoda
            Nov 9 at 5:07













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          The Firebase on() method can fire multiple times: once when it initially loads the data, and again whenever the data changes. Since a promise (the thing you call then() on) can only resolve once, on() can't return a promise.



          There are two options here:





          1. You want to only load the data once.



            If this is the case, you should use Firebase's once() method, which does return a promise.



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .once('value').then(snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            return false;
            }
            })
            }
            }).then(value => {
            // TODO: perform subsequent action on boolean value
            })



          2. You want to listen for changes on the data too.



            If this is the case, you should put the subsequent action you want to take into the on() callback:



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .on('value', snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            }
            })
            // TODO: perform subsequent action on data
            }
            })



          Note that both of these operations look pretty expensive for what they're trying to accomplish: scanning a JSON tree for a specific value is an anti-pattern in Firebase, and typically means you should modify/augment your JSON to allow a direct lookup or query.



          For example, I suspect you now have a structure like /Users/$randomkey/$uid: { ..user data... }. For better performance, consider storing the user data directly under their UID: /Users/$uid: { ..user data... }. This removes the need for a query, and allows you to directly load the data for a user from this.oAngularFireDatabase.database.ref('Users').child(user.uid).






          share|improve this answer












          The Firebase on() method can fire multiple times: once when it initially loads the data, and again whenever the data changes. Since a promise (the thing you call then() on) can only resolve once, on() can't return a promise.



          There are two options here:





          1. You want to only load the data once.



            If this is the case, you should use Firebase's once() method, which does return a promise.



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .once('value').then(snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            return false;
            }
            })
            }
            }).then(value => {
            // TODO: perform subsequent action on boolean value
            })



          2. You want to listen for changes on the data too.



            If this is the case, you should put the subsequent action you want to take into the on() callback:



            this.oAngularFireDatabase.database.ref('Users').orderByKey()
            .on('value', snapshot => {
            if (snapshot.hasChildren()) {
            snapshot.forEach(innerSnap => {
            if (innerSnap.hasChild(user.uid)) {
            //User role key
            this.loggedInUserUserRoleKey = innerSnap.key;
            //User id
            this.loggedInUserId = user.uid;

            //User name
            this.loggedInUserName = innerSnap.child(user.uid).child("user_name").val();

            if (innerSnap.child(user.uid).hasChild("user_image")) {
            //User Image
            this.loggedInUserImage = innerSnap.child(user.uid).child("user_image").val();
            }
            }
            })
            // TODO: perform subsequent action on data
            }
            })



          Note that both of these operations look pretty expensive for what they're trying to accomplish: scanning a JSON tree for a specific value is an anti-pattern in Firebase, and typically means you should modify/augment your JSON to allow a direct lookup or query.



          For example, I suspect you now have a structure like /Users/$randomkey/$uid: { ..user data... }. For better performance, consider storing the user data directly under their UID: /Users/$uid: { ..user data... }. This removes the need for a query, and allows you to directly load the data for a user from this.oAngularFireDatabase.database.ref('Users').child(user.uid).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 14:44









          Frank van Puffelen

          218k25361386




          218k25361386












          • you saved my time brother @Frank van Puffelen
            – ThusharaHettigoda
            Nov 9 at 5:03












          • i have structure like Users/$UserRoleKey/$uid:{}
            – ThusharaHettigoda
            Nov 9 at 5:07


















          • you saved my time brother @Frank van Puffelen
            – ThusharaHettigoda
            Nov 9 at 5:03












          • i have structure like Users/$UserRoleKey/$uid:{}
            – ThusharaHettigoda
            Nov 9 at 5:07
















          you saved my time brother @Frank van Puffelen
          – ThusharaHettigoda
          Nov 9 at 5:03






          you saved my time brother @Frank van Puffelen
          – ThusharaHettigoda
          Nov 9 at 5:03














          i have structure like Users/$UserRoleKey/$uid:{}
          – ThusharaHettigoda
          Nov 9 at 5:07




          i have structure like Users/$UserRoleKey/$uid:{}
          – ThusharaHettigoda
          Nov 9 at 5:07


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206459%2fcalling-then-function-after-fetching-data-from-firebase-in-ionic-3%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

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff