I'm getting this error, when trying to get response from my service, in which i have placed my Ajax call! I want the controller code to delay that is why i am returning a promise from service. But I am getting .then of undefined
error.
Here is the code
app.service('abc', function(){
var function() {
return $http({method : "POST",
url :"url",
data:{
a}
}).then(function mySuccess(response) {
return response;
})
}
});
in controller
var promise=servicename.functionname();
promise.then(res)
{}
.then of undefined
is the error I am getting. Any solution?
Update service and inject $http
app.service('abc', function ($http) {
this.functionName = function (data) {
return $http({
method: "POST",
url: "url",
data: data
}).then(function mySuccess(response) {
return response.data;
})
};
});
Controller
app.controller('myController', function (abc) {
abc.functionName({
a: 'a'
}).then((data) => console.log(data))
});
Here the code will not wait the return inside then() and it will execute the code ahead. And as it does no find any return statement it will return undefined. To fix this in the service function you will return just the promise so your return statement will look something like this
return $http({
method : "POST",
url :"url",
data:{a}
})
The controller code will be same
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.