1

I am trying to return an object from a $rootScope function called retrieveUser() in AngularJS. The object is returned. I have run console.log() on the response of the function ran when $http is successful. Here is my $rootScope function:

    $rootScope.retrieveUser = function() {

        var apiUrl = "http://104.251.218.29:8080";

        if($cookies.get('tundraSessionString')) {

            var cookie = $cookies.get('tundraSessionString');

            $http({
                method: "POST",
                url: apiUrl + "/api/master/v1/auth/checkauth",
                data: "sessionString=" + cookie,
                headers: {
                    'Content-Type' : 'application/x-www-form-urlencoded;',
                    'Cache-Control': 'no-cache'
                }
            }).then(function mySuccess(response) {

                if(response.data.event == "error") {

                    window.location = "/auth/logout";

                } else {

                    return response.data;

                }


            })

        } else {

            window.location = "/auth/login";

        }

    };

With this method, I access it in my controller such as this (and console.log() just to test my work):

vm.user = $rootScope.retrieveUser();
console.log($rootScope.retrieveUser());

But, I have yet to get this to work. I have tried specifying specific objects in an array in my $rootScope function. I know it runs, because I have the $rootScope consoling something when it is run, and it shows a console.log() of the response of the $http request. It looks like this:

Object {event: "success", table: Object}
event:"success"
table:Object
__proto__:Object

Yet, when I console.log() the vm.user with the function $rootScope.retrieveUser(), even though the function is supposed to be returning the object, I simply receive "undefined".

I have been banging my head on this for days, read some articles on functions/objects and I still cannot figure this out. We're two days in.

3 Answers 3

1

try this:

        if($cookies.get('tundraSessionString')) {

            var cookie = $cookies.get('tundraSessionString');

            //return a promise
            return $http({
                method: "POST",
                url: apiUrl + "/api/master/v1/auth/checkauth",
                data: "sessionString=" + cookie,
                headers: {
                    'Content-Type' : 'application/x-www-form-urlencoded;',
                    'Cache-Control': 'no-cache'
                }
            }).then(function mySuccess(response) {

                if(response.data.event == "error") {

                    window.location = "/auth/logout";

                } 
                else {

                    return response.data;

                }


            })

        } 
        else {

            window.location = "/auth/login";

        }

and

$rootScope.retrieveUser().then(function(user){vm.user = user;})
Sign up to request clarification or add additional context in comments.

1 Comment

This worked! I will remember to use the promise method from now on. I'm new to Angular. First app I've built. But I've gotten pretty far without a hiccup. Thanks for the help!
1

What you are returning from retrieveUser when your cookie is set is what $http returns, which is a promise. Try this:

$rootScope.retrieveUser().then(function(user){vm.user = user;})

3 Comments

TypeError: Cannot read property 'then' of undefined was in the Chrome console log.
The retrieveUser function should return a promise when using the .then approach. You can return the $http.get (which aso uses a promise)
yeah whoops I'm used to coffeescript, you also need to change it to return $http({...
0

retrieveUser fn doesn't return your data :) $http is asynchronous function and you should read about promises

function handleUser(user){
//do something
}

function retrieveUser(callback){
  $http({...}).then(function(response){
    callback(response.data.user);
  });
}
//how to use it:

retrieveUser(handleUser);

but first of all you may need a service for getting some data instead of using $rootScope

and secondly you can pass a user in your template in script tag then you don't need another http request and user will be globaly available

<script>var user=<?php echo json_encode($user);?></script>

Comments

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.