0

I am a new bie to angular and trying to learn it. I am trying to display user data from a webservice that I received as response. here's the response

    {
    "Users": {
        "SearchTerm": "angularjs ra1",
        "ResultsPerPage": "20",
        "RecordStartNumber": "0",
        "TotalEstimatedResults": "6",
        "User": [{
            "WWID": "123456",
            "Email": "[email protected]",
            "BusinessTitle": "Project Manager",
            "Name": "Mary Wilson",
            "firstname": "Mary",
            "lastname": "Wilson",
            "idsid": "RAP"
        }, {
            "WWID": "1234567",
            "Email": "[email protected]",
            "BusinessTitle": "Quality Assurance",
            "Name": "Steve Smith",
            "firstname": "Steve",
            "lastname": "Smith",
            "idsid": "DRPRESTO"
        }, {
            "WWID": "12345678",
            "Email": "[email protected]",
            "BusinessTitle": "Chef",
            "Name": "Jon Jones",
            "firstname": "Jon",
            "lastname": "Jones",
            "idsid": "JJONES"
        }]
    }
}

MyScript.js

    var Application = angular.module('TestModule', []);
Application.controller('TestController', function ($scope,$http) {
    $scope.TestValue = "Hello World from Controller";
    $scope.Myfunc = function asdf() {
        $http.get('http://unifiedcollaborationprofile.gots.com/search/expert/data/v2/?apiKey=SECRET&listFromPersonNumber=0&numPeoplePerPage=20&query=angularjs+ra1&returnFriendlyResults=true').then(
            function (response) {
                $scope.users = [];
                angular.forEach(response.Users, function (value, key) {
                    $scope.users.push(value);
                });
            });
    };
});

Page.html

<!DOCTYPE html>
<html ng-app="TestModule">
<head>
    <title></title>
    <meta charset="utf-8" />
</head>
{{2+3}}
<body>
    <div ng-controller="TestController">
        {{2+3}}
        {{TestValue}}
        <input type="text" ng-model="TestValue" />
        <input type="button" value="Click Me To Get All Users!!!!" ng-click="Myfunc()" />
        <ul ng-repeat="k in users">
            <li>WWID:{{k.WWID}}  Email:{{k.Email}}  Name:{{k.Name}}  IDSID:{{k.idsid}}</li>
        </ul>

    </div>
</body>
</html>
<script src="scripts/angular.min.js"></script>
<script src="scripts/MyScripts/MyScript.js"></script>

But nothing gets rendered on my Page. I am not sure if I am rightly accessing the nested User object in Users Collection of JSON and its properties. what am I missing.

8
  • It's possible I'm reading it wrong, but it looks like you're trying to loop through the Users level of the JSON, which isn't actually an array. It looks like the Users property has a couple of random properties, then another property User which holds the array of users Commented Apr 7, 2017 at 23:25
  • I declared users as an array in the code $scope.users = [];.what do you mean by its not an array? Commented Apr 7, 2017 at 23:27
  • Aah sorry, the Users level of the JSON data is not an array -> You have "Users": {...} - the data at this level is not an array. Does that make sense? Commented Apr 7, 2017 at 23:28
  • Yeah, could be how do we deal with these then? Commented Apr 7, 2017 at 23:30
  • I think this is what TypedSource was trying to mention below - instead of doing angular.forEach(response.Users... do angular.forEach(response.Users.User -> the key difference is the extra .User at the end. You've already tried that? Commented Apr 7, 2017 at 23:32

3 Answers 3

2

I've created plunker with your code and all I did to make it work is to change it to this:

$scope.Myfunc = function asdf() {
    $http.get('test.json').then(function(response) {
      $scope.users = [];
      angular.forEach(response.data.Users.User, function(value, key) {
        $scope.users.push(value);
      });
    });
  };

http://plnkr.co/edit/Fu1tCnhxSn9dgBXtTJ9r?p=preview

You must remember that then after http promise gets resolved receives an object that contains several values: status code, headers and data that will contain your data

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

1 Comment

This was the issue, FYI - adding .data
1

I think problem is here

 $scope.users = [];
                angular.forEach(response.Users, function (value, key) {
                    $scope.users.push(value);
                });

try change to

$scope.users = response.data.Users

or you can change to

 $scope.users = [];
                    angular.forEach(response.data.Users, function (value, key) {
                        $scope.users.push(value);
                    });

4 Comments

this just gives list items equal to number of users returned by the service but no data yet..:(
@Programmerzzz in your html , try change to k.user.WWID
can not be. definitly is he has to access response.Users.User because here is the array he wants to access
@Programmerzzz dont change anything in html and in js try change to response.data.Users.User , it work well
1
angular.forEach(response.Users.User, function (value, key) {
 $scope.users.push(value);
});

your array is nested so you have to call the array in the object

3 Comments

sure ur response is what you exprect to get and it's json object and not json string?
I think its a JSON string, you mean we need to parse it before we do the processing on it using forEach loop?
console.log(response) if it is displayed as object what you can expand, you dont need to parse. otherwhise JSON.parse(response) before the loop

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.