Reputation: 367
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;
});
Upvotes: 0
Views: 308
Reputation: 23035
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.
Upvotes: 2