Not able to catch error for .on() in firebase












0















I am using below code to retrieve a value



try {
discountFbRef.orderByChild('discode').equalTo(code).on("value", function(snapshot) {
console.log('snapshot val discode', snapshot.val().discode);

if (snapshot.exists()) {
snapshot.forEach(function(data) {

$scope.discountApplicable.id = data.val().id;


});
} else {
console.log('It doesnt exist');
}
}, function(error) {
console.log(error);
});
} catch (error) {
console.log('error occured during search', error);
}


When there is value equal to the search string it's working fine. But when I try to find a keyword that doesn't exist in "discode", It throws




Uncaught TypeError: Cannot read property 'discode' of null




For some reason even though I try to catch the error with try-catch and error function I am not able to catch it.
I need a way to handle the error and show message that search string doesn't exist.










share|improve this question





























    0















    I am using below code to retrieve a value



    try {
    discountFbRef.orderByChild('discode').equalTo(code).on("value", function(snapshot) {
    console.log('snapshot val discode', snapshot.val().discode);

    if (snapshot.exists()) {
    snapshot.forEach(function(data) {

    $scope.discountApplicable.id = data.val().id;


    });
    } else {
    console.log('It doesnt exist');
    }
    }, function(error) {
    console.log(error);
    });
    } catch (error) {
    console.log('error occured during search', error);
    }


    When there is value equal to the search string it's working fine. But when I try to find a keyword that doesn't exist in "discode", It throws




    Uncaught TypeError: Cannot read property 'discode' of null




    For some reason even though I try to catch the error with try-catch and error function I am not able to catch it.
    I need a way to handle the error and show message that search string doesn't exist.










    share|improve this question



























      0












      0








      0








      I am using below code to retrieve a value



      try {
      discountFbRef.orderByChild('discode').equalTo(code).on("value", function(snapshot) {
      console.log('snapshot val discode', snapshot.val().discode);

      if (snapshot.exists()) {
      snapshot.forEach(function(data) {

      $scope.discountApplicable.id = data.val().id;


      });
      } else {
      console.log('It doesnt exist');
      }
      }, function(error) {
      console.log(error);
      });
      } catch (error) {
      console.log('error occured during search', error);
      }


      When there is value equal to the search string it's working fine. But when I try to find a keyword that doesn't exist in "discode", It throws




      Uncaught TypeError: Cannot read property 'discode' of null




      For some reason even though I try to catch the error with try-catch and error function I am not able to catch it.
      I need a way to handle the error and show message that search string doesn't exist.










      share|improve this question
















      I am using below code to retrieve a value



      try {
      discountFbRef.orderByChild('discode').equalTo(code).on("value", function(snapshot) {
      console.log('snapshot val discode', snapshot.val().discode);

      if (snapshot.exists()) {
      snapshot.forEach(function(data) {

      $scope.discountApplicable.id = data.val().id;


      });
      } else {
      console.log('It doesnt exist');
      }
      }, function(error) {
      console.log(error);
      });
      } catch (error) {
      console.log('error occured during search', error);
      }


      When there is value equal to the search string it's working fine. But when I try to find a keyword that doesn't exist in "discode", It throws




      Uncaught TypeError: Cannot read property 'discode' of null




      For some reason even though I try to catch the error with try-catch and error function I am not able to catch it.
      I need a way to handle the error and show message that search string doesn't exist.







      javascript firebase firebase-realtime-database error-handling






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 2:44









      zynkn

      3,90821030




      3,90821030










      asked Nov 22 '18 at 2:40









      VamsigaVamsiga

      13




      13
























          1 Answer
          1






          active

          oldest

          votes


















          0














          So, the functions that are passed to the second and third parameters of .on are called a callback functions. That means they might get called after (asynchronously) the code you've posted has executed. You'll want to move your try-catch inside of the callback function.



          function handleValueSuccess(snapshot) {
          try {
          console.log('snapshot val discode', snapshot.val().discode);

          if (snapshot.exists()) {
          snapshot.forEach(function(data) {
          $scope.discountApplicable.id = data.val().id;
          });
          } else {
          console.log("It doesn't exist");
          }
          } catch (error) {
          console.log('error occurred during search', error);
          }
          }

          function handleValueError(error) {
          console.log('error occurred on value', error);
          }

          discountFbRef
          .orderByChild('discode')
          .equalTo(code)
          .on("value", handleValueSuccess, handleValueError);





          share|improve this answer


























          • I moved them to the call back function it didn't catch errors. I'm not sure how would it work because there is a console.log() inside the call back it doesn't log anything. May be I did something wrong could you show the code based on my code?

            – Vamsiga
            Nov 22 '18 at 4:42













          • @Vamsiga I edited my answer to show what I was thinking. Does that not work for you?

            – Matthew Herbst
            Nov 22 '18 at 4:51






          • 1





            Thanks it worked, I put try catch after the console.log() which is doing the snapshot.val().discode.

            – Vamsiga
            Nov 22 '18 at 5:00











          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',
          autoActivateHeartbeat: false,
          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%2f53423136%2fnot-able-to-catch-error-for-on-in-firebase%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









          0














          So, the functions that are passed to the second and third parameters of .on are called a callback functions. That means they might get called after (asynchronously) the code you've posted has executed. You'll want to move your try-catch inside of the callback function.



          function handleValueSuccess(snapshot) {
          try {
          console.log('snapshot val discode', snapshot.val().discode);

          if (snapshot.exists()) {
          snapshot.forEach(function(data) {
          $scope.discountApplicable.id = data.val().id;
          });
          } else {
          console.log("It doesn't exist");
          }
          } catch (error) {
          console.log('error occurred during search', error);
          }
          }

          function handleValueError(error) {
          console.log('error occurred on value', error);
          }

          discountFbRef
          .orderByChild('discode')
          .equalTo(code)
          .on("value", handleValueSuccess, handleValueError);





          share|improve this answer


























          • I moved them to the call back function it didn't catch errors. I'm not sure how would it work because there is a console.log() inside the call back it doesn't log anything. May be I did something wrong could you show the code based on my code?

            – Vamsiga
            Nov 22 '18 at 4:42













          • @Vamsiga I edited my answer to show what I was thinking. Does that not work for you?

            – Matthew Herbst
            Nov 22 '18 at 4:51






          • 1





            Thanks it worked, I put try catch after the console.log() which is doing the snapshot.val().discode.

            – Vamsiga
            Nov 22 '18 at 5:00
















          0














          So, the functions that are passed to the second and third parameters of .on are called a callback functions. That means they might get called after (asynchronously) the code you've posted has executed. You'll want to move your try-catch inside of the callback function.



          function handleValueSuccess(snapshot) {
          try {
          console.log('snapshot val discode', snapshot.val().discode);

          if (snapshot.exists()) {
          snapshot.forEach(function(data) {
          $scope.discountApplicable.id = data.val().id;
          });
          } else {
          console.log("It doesn't exist");
          }
          } catch (error) {
          console.log('error occurred during search', error);
          }
          }

          function handleValueError(error) {
          console.log('error occurred on value', error);
          }

          discountFbRef
          .orderByChild('discode')
          .equalTo(code)
          .on("value", handleValueSuccess, handleValueError);





          share|improve this answer


























          • I moved them to the call back function it didn't catch errors. I'm not sure how would it work because there is a console.log() inside the call back it doesn't log anything. May be I did something wrong could you show the code based on my code?

            – Vamsiga
            Nov 22 '18 at 4:42













          • @Vamsiga I edited my answer to show what I was thinking. Does that not work for you?

            – Matthew Herbst
            Nov 22 '18 at 4:51






          • 1





            Thanks it worked, I put try catch after the console.log() which is doing the snapshot.val().discode.

            – Vamsiga
            Nov 22 '18 at 5:00














          0












          0








          0







          So, the functions that are passed to the second and third parameters of .on are called a callback functions. That means they might get called after (asynchronously) the code you've posted has executed. You'll want to move your try-catch inside of the callback function.



          function handleValueSuccess(snapshot) {
          try {
          console.log('snapshot val discode', snapshot.val().discode);

          if (snapshot.exists()) {
          snapshot.forEach(function(data) {
          $scope.discountApplicable.id = data.val().id;
          });
          } else {
          console.log("It doesn't exist");
          }
          } catch (error) {
          console.log('error occurred during search', error);
          }
          }

          function handleValueError(error) {
          console.log('error occurred on value', error);
          }

          discountFbRef
          .orderByChild('discode')
          .equalTo(code)
          .on("value", handleValueSuccess, handleValueError);





          share|improve this answer















          So, the functions that are passed to the second and third parameters of .on are called a callback functions. That means they might get called after (asynchronously) the code you've posted has executed. You'll want to move your try-catch inside of the callback function.



          function handleValueSuccess(snapshot) {
          try {
          console.log('snapshot val discode', snapshot.val().discode);

          if (snapshot.exists()) {
          snapshot.forEach(function(data) {
          $scope.discountApplicable.id = data.val().id;
          });
          } else {
          console.log("It doesn't exist");
          }
          } catch (error) {
          console.log('error occurred during search', error);
          }
          }

          function handleValueError(error) {
          console.log('error occurred on value', error);
          }

          discountFbRef
          .orderByChild('discode')
          .equalTo(code)
          .on("value", handleValueSuccess, handleValueError);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 17:50

























          answered Nov 22 '18 at 2:47









          Matthew HerbstMatthew Herbst

          10.6k134687




          10.6k134687













          • I moved them to the call back function it didn't catch errors. I'm not sure how would it work because there is a console.log() inside the call back it doesn't log anything. May be I did something wrong could you show the code based on my code?

            – Vamsiga
            Nov 22 '18 at 4:42













          • @Vamsiga I edited my answer to show what I was thinking. Does that not work for you?

            – Matthew Herbst
            Nov 22 '18 at 4:51






          • 1





            Thanks it worked, I put try catch after the console.log() which is doing the snapshot.val().discode.

            – Vamsiga
            Nov 22 '18 at 5:00



















          • I moved them to the call back function it didn't catch errors. I'm not sure how would it work because there is a console.log() inside the call back it doesn't log anything. May be I did something wrong could you show the code based on my code?

            – Vamsiga
            Nov 22 '18 at 4:42













          • @Vamsiga I edited my answer to show what I was thinking. Does that not work for you?

            – Matthew Herbst
            Nov 22 '18 at 4:51






          • 1





            Thanks it worked, I put try catch after the console.log() which is doing the snapshot.val().discode.

            – Vamsiga
            Nov 22 '18 at 5:00

















          I moved them to the call back function it didn't catch errors. I'm not sure how would it work because there is a console.log() inside the call back it doesn't log anything. May be I did something wrong could you show the code based on my code?

          – Vamsiga
          Nov 22 '18 at 4:42







          I moved them to the call back function it didn't catch errors. I'm not sure how would it work because there is a console.log() inside the call back it doesn't log anything. May be I did something wrong could you show the code based on my code?

          – Vamsiga
          Nov 22 '18 at 4:42















          @Vamsiga I edited my answer to show what I was thinking. Does that not work for you?

          – Matthew Herbst
          Nov 22 '18 at 4:51





          @Vamsiga I edited my answer to show what I was thinking. Does that not work for you?

          – Matthew Herbst
          Nov 22 '18 at 4:51




          1




          1





          Thanks it worked, I put try catch after the console.log() which is doing the snapshot.val().discode.

          – Vamsiga
          Nov 22 '18 at 5:00





          Thanks it worked, I put try catch after the console.log() which is doing the snapshot.val().discode.

          – Vamsiga
          Nov 22 '18 at 5:00


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423136%2fnot-able-to-catch-error-for-on-in-firebase%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin