1

I created a form and onclick of the form i want to see the response from the server(nosejs). In Angular Controller Im calling the service. The response is visible when called from the service itself, but when called from the controller nothing shows up.

Controller:

MyApp.angular.controller("loginCtrl", function($scope, UserService){
    $scope.name = "TestName";

    $scope.check = function(user){

        UserService.createUser(user, function(response){
            $scope.userinfo = response;
            console.log("FROM Ctrlr: " + response);
        });
    }

});

Service:

MyApp.angular.factory("UserService", function($http,$location){

        var service = {
            createUser: createUser
        };

        return service;

        function createUser(user){
            $http.post("/register", user).then(function(response){

            console.log("FROM Srvc:" +response);
            });
        }
});

NodeJS:

app.post("/register", function(req, res){
    var user = req.body;
res.json(user);
});

I can see the response logged to the console if I call it from the Service. But no response when called from the controller itself. Reason to include MyApp.angular.factory and MyApp.angular.controller is I have declared MyApp in another config file.

What am I doing wrong?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Somename
  • 3,376
  • 16
  • 42
  • 84
  • 1
    `UserService.createUser` method does not return anything nor does it receive a callback to handle. How can you expect `response` object in your controller if you haven't returned it somehow? – Emin Laletovic Mar 08 '17 at 23:27
  • No offense brother/sister, but perhaps before diving into a framework like Angular, you might want to first become familiar with the JavaScript language as a whole. That being said, if you absolutely must/want to start digging into Angular, find a good example project that covers topics you are interested in, and emulate what they do until you understand what's going on. – Pytth Mar 08 '17 at 23:58
  • @Pytth thank you. To learn JS as a whole will take a long time. But thanks. I learned about Angular promises and thn cause of this question. But thanks. I see what you mean. – Somename Mar 09 '17 at 19:54

3 Answers3

1

In order to get the response in your controller from the factory change your createUser func in your factory like this:

function createUser(user){
            //notice the return here!!!
            return $http.post("/register", user).then(function(response){

            console.log("FROM Srvc:" +response);
            return response.data; //notice the return here too!!!
            });
        }

You need to return the promise inside the function and inside the then return the result as well in order to get access to it from wherever you call the function.

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
0

Unlike node.js which uses callback-based APIs, the AngularJS framework uses promise-based APIs. Return the promise and use its .then method:

Controller

app.controller("loginCtrl", function($scope, UserService){
    $scope.name = "TestName";

    $scope.check = function(user){

        //UserService.createUser(user, function(response){
        //USE .then method
        return UserService.createUser(user).then(function(data) {
            $scope.userinfo = data;
            console.log("FROM Ctrlr: " + data);
            return data;
        }).catch(function(errorResponse) {
            console.log(errorResponse.status);
            throw errorResponse;
        });
    }

});

Service

    function createUser(user){
        var promise = $http.post("/register", user).then(function(response){
            var data = response.data;
            console.log("FROM Srvc:" +data);
            //RETURN data to chain
            return data;
        });
        //RETURN promise
        return promise;
    }

Don't convert promise-based APIs to callback-type APIs. It's considered an anti-pattern.

See Why are Callbacks from Promise .then Methods an Anti-Pattern.

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95
-1

You need to add 1 more parameter, callback and return the response to the callback function

 function createUser(user, callback){
        $http.post("/register", user).then(function(response){

        console.log("FROM Srvc:" +response);

         callback(response);  // return the response in the callback function
        });
 }
digit
  • 4,479
  • 3
  • 24
  • 43
  • why my answer get downvote. Hey, downvoter, show yourself – digit Mar 09 '17 at 03:25
  • Don't convert promise-based APIs to callback-type APIs. It's considered an anti-pattern. See [Why are Callbacks from Promise `.then` Methods an Anti-Pattern.](http://stackoverflow.com/questions/35660881/why-are-callbacks-from-promise-then-methods-an-anti-pattern) – georgeawg Mar 10 '17 at 18:02