Question

on my server side (ASP.ne MVC) I have method which looks like this:

    [HttpGet]
    public JsonResult GetTrenings(string treningId)
    {
        var tempId = Guid.Parse(treningId);
        var trening = TreningService.GetTreningById(tempId);
        _trenings = TreningService.GetAllTreningsForUser(trening.UserId);
        return Json(_trenings, JsonRequestBehavior.AllowGet);

    }

And I have Angular service :

publicApp.angularModule.factory('feedSingleTreningService', function ($q, $http) {

return {
   getTrenings: function (data) {
       var input = $http.get("/Feed/GetTrenings", { params: { treningId: data } });


        var deferred = $q.defer();

        deferred.resolve(input);
        return deferred.promise;
    },

};
});

And In my Controller I call this service like this:

 feedSingleTreningService.getTrenings(data).then(function(results) {
        console.log("test", results);
    });

But nothing is shown in console, I've debugged server side and request reaches it, and it returns _trenings, also service returns promise, but nothing happens.

I've changed then to finally, and in console "test" was shown but results were undefined.

Why is this happening ?

Was it helpful?

Solution

You don't need to defer your call to $http because it already returns a promise.

Just return

return $http.get("/Feed/GetTrenings", { params: { treningId: data } });

then anything that calls your function can do:

getTrenings(myData).then(function(data) { do something }).fail(function() { error });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top