AJ_
AJ_

Reputation: 3987

How to Iterate over angular json object

I have created a nodejs application that will read all the databases in my mongo Db. Which i am able to do in the console. However, when i try too get the data parsed to a json object and dispaly it to the screen, i cant manage to get the info to display. Hopeing someone can help me figure out how or tell me what im doing wrong. Thanks

app.js

// listen for get request, aka transfers the info in mongo to client
app.get('/databases', function (req, res) {
    console.log("-- recived GET request --"); 
    db.open(function(err, db) {

      // Use the admin database for the operation
      var adminDb = db.admin();

      // List all the available databases
      adminDb.listDatabases(function(err, dbs) {
        assert.equal(null, err);
        assert.ok(dbs.databases.length > 0);
        console.log(dbs);
        res.json(dbs); 
        db.close();
      });
    });
}); 

controller.js

var app = angular.module('myApp', []);

app.controller('customersCtrl', function($scope, $http) {
    console.log("controller connected");

function refresh(){ 
// create route 
$http.get('/databases').success(function(response) {
    console.log("recived data requested");
    $scope.databases = response; 
  });
}

// Call refresh to get req
refresh(); 

});// Controller 

index.html

<body>

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

<ul>
  <li ng-repeat="contact in databases">
    {{ contact }}
  </li>
</ul>
 <button type="button" onclick="hitMe()">Click Me!</button> 
</div>

</body>

enter image description here

Upvotes: 3

Views: 131

Answers (2)

Henry00
Henry00

Reputation: 421

As Andrew said try contact.name in your html code.

"contact" on its own would be the entire object. you have to specify what part of the object you want to use.

Upvotes: 0

punov
punov

Reputation: 828

Looks like you need to iterate through datebases.databases object. As soon as $scope.databases is:

{
 databases: [],
 totalSize: ..,
 ..
}

you need the following ng-repeat on your page:

<ul>
  <li ng-repeat="contact in databases.databases">
    {{ contact.name }}
  </li>
</ul>

Upvotes: 2

Related Questions