0

I am trying to populate a basic angular app with data from a database. Example similar to that in w3schools. This is the code so far:

<div ng-app="myApp" ng-controller="customersCtrl">

    <table>
        <tr ng-repeat="x in names">
            <td>{{ x.id }}</td>
            <td>{{ x.appid }}</td>
        </tr>
    </table>
</div>    

<script>
    var app = angular.module('myApp', []);
        app.controller('customersCtrl', function($scope, $http) {
            $http.get("http://localhost:3000/test")
            .then(function (response) {$scope.names = response.data.records;}); 
        });
</script>

However, no data are retrieved. The response i get from http://localhost:3000/test is the following:

[

{"id":1,"appid":1},{"id":2,"appid":1},{"id":7,"appid":1}, {"id":20,"appid":1},{"id":38,"appid":1},{"id":39,"appid":1},{"id":40,"appid":1},{"id":41,"appid":3},{"id":44,"appid":3},{"id":70,"appid":2},{"id":71,"appid":2},{"id":72,"appid":2}

]

I checked it both from the browser and from postman. If I replace response.data.records with the response above, the table displays the values in the response.

I know I must be messing something up, but somehow I am missing it. My best guess would be that of the format of the response.

2 Answers 2

2

You need to remove .records from the respnse.data

$http.get("http://localhost:3000/test")
        .then(function (response) {$scope.names = response.data;}); 

Since you are directly receiving an array just assign array from response directly to $scope.names.

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

1 Comment

What a coincidence, I just figured it out. Anyway I got it via trial and error so thanks for the explanation.
2

According to your shown data your issue should be solved when you replace

$scope.names = response.data.records;

with

$scope.names = response.data;

because your sample response is just an array without being wrapped within a record property.

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.