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?

3
  • 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? Commented Mar 8, 2017 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. Commented Mar 8, 2017 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. Commented Mar 9, 2017 at 19:54

3 Answers 3

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.

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

Comments

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.

Comments

-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
        });
 }

2 Comments

why my answer get downvote. Hey, downvoter, show yourself
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.