0

I have a basic controller defined in C#, its only purpose is to return whoever the current user is.

    namespace ProjectName.Controllers
{
    public class UserController : ApiController
    {


        // GET api/user/
        public string Get()
        {
            return User.Identity.Name;
        }

    }
}

I then have an ngResource defined to look at that controller

cript.factory('User', function ($resource) {
return $resource('/api/User')
});

finally I try to get that user in my Angular Controller

    $scope.checkIfAuth = function () {
    $scope.user = User.get();

    console.log($scope.user);

    if ($scope.user === '"DS//:lemieszr"') {
        console.log("success");
    }
};

The problem is $scope.user is a resource object that looks like this

    Resource {$get: function, $save: function, $query: function, $remove: function, $delete: function}
    0: """
    1: "D"
    2: "S"
    3: "\"
    4: "\"
    5: "l"
    6: "e"
    7: "m"
    8: "i"
    9: "e"
    10: "s"
    11: "z"
    12: "r"
    13: """
    __proto__: Resource

Is there anyway to just get the string containing my Username. User.query() only returns an empty array.

Robert Lemiesz
  • 1,026
  • 2
  • 17
  • 29

2 Answers2

1

I would advise looking into Angular's implementation of Kris Kowal's Q promise library ($q). I'm working with mvc 4.0 right now as well and I have found it to be much more flexible than ngResource.

However with respect to your question specifically, your issue here is you are making an async request to your server while synchronously invoking its response. Well, the data hasn't gotten there yet (why you are seeing an empty array []). That empty array is basically the server saying "Hey, good work following protocol on your http request, we are getting your data, to prove it to you here is an empty array where we will put the data there when it gets to you."

In other words, your problem is that you are asking setting checking the value of $scope.user before it even has one (roughly speaking).

You two options for simple fixes (at least to my knowledge): You can set $scope.user to the response from the server in a SEPARATE function, or some other service, and then use the response once it gets there. OR (the better 'simple' fix) use the $timeout service, which essentially tells your system to "chill until my async request gets done and I have my data."

hunt
  • 78
  • 1
  • 10
  • Thank you other answers were good to, but you were the only one that mentioned the $timeout. Ive seen some example of the $q library and but I stuck with ngResource for simplicity – Robert Lemiesz Jun 20 '13 at 18:21
1

User.query() returns an empty array because it needs an array of json objects [{...},{...}] as a response from your back-end. User.get assumes that your server returns an object like {"name":"Andi"} for example, so if your server response is correct there is no problem with get method.

$scope.user = User.get();

Than you have access to you name property as $scope.user.name

This plumk may help you link

arussinov
  • 1,237
  • 11
  • 16