0

I have a service function that looks like:

this.addJobRating = function(jobId, userId, rating){
    var deferred = $q.defer();
    $http.post('/to/my/api', {job_id: jobId, user_id: userId, rating: rating})
        .success(function(data){
            deferred.resolve(data)
        })
        .error(function(data){
            deferred.reject('promise call failed');
        });
    return deferred;
};

I can call the above in my controller like so

console.log(myService.addJobRating(646, 9999, 'good-result')

This logs the expected promise object in console log. But if I try to resolve the promise using:

myService.addJobRating(646, 9999, 'good-result').then(function(data){
    console.log(data);
}, function(data){
    console.log(data);
});

I get an TypeError: undefined is not a function error.

Why is this happening?

1
  • 3
    Leaving the typo apart, why can't you just return the http promise itself? return $http.post('/to/my/api', {job_id: jobId, user_id: userId, rating: rating}).then(function(resp){ return resp.data; }, function(data){ return $q.reject('promise call failed'); }); Commented Sep 18, 2014 at 18:55

2 Answers 2

4

myService.addJobRating should return deferred.promise.

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

Comments

3

You should be returning the promise - deferred.promise - from addJobRating, not deferred.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.