0

I have an error in line callback(response.data) telling that callback is not a function. Well, I am new to this and I am trying to figure out how to fix this issue but I am having a tough time. Any idea is appreciated. Thanks

 app.service('NotificationPollService', function ($q, $http, $timeout) {

      var notification = {};
      notification.poller = function (callback, error) {
          return $http.get('api/sample.php').then(function (response) {
              if (typeof response.data === 'object') {
                  callback(response.data);                 
              } else {
                  error(response.data);
              }
              $timeout(notification.poller, 2000);
          });
      }

      notification.poller();

      return notification;
});
1
  • You are not passing any function to notification.poller();. Commented Oct 22, 2017 at 3:16

1 Answer 1

2

You declared poller as a function that receives two functions as its parameters, but you are invoking it with no parameters in two different places:

    notification.poller();

and

    $timeout(notification.poller, 2000);

Remember you can log the value of the variables to the console as console.log(callback, error), in which case you will see it prints undefined, undefined.

What was the intention of invoking notification.poller() in that line? Looks like that function should be called by the user of your service instead, like this:

    notification.poller(data => console.log(`Received some data! ${data}`),
                        error => console.error(`Some error occurred: ${error}`));

Lastly, what is the intention of poller? By the name I imagined it was going to invoke that endpoint some fixed X number of times (polling) and after X failures it would give up, but it's always invoking poller again, even after a success.

Sign up to request clarification or add additional context in comments.

2 Comments

it will be called to my controller. passing callback into notification.poller(callback) gives an error callback is undefined. anyway thanks for your help.
updated the answer, there are 2 places where that function is invoked with no arguments. Also, you cannot invoke it like you want: notification.poller(callback) because it takes 2 parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.