3

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

3
  • Did you try and add another ng-repeat, and then repeat throught contract.names or contact.Sizeof Commented Oct 26, 2015 at 14:49
  • can step through your code and see how your response look like? In same cases you'll need to get data property from response: $scope.databases = response.data; (docs.angularjs.org/api/ng/service/$http#general-usage - response object) Commented Oct 26, 2015 at 14:59
  • Yeah Andrew i tried that. didn’t work. punov had the correct answer Commented Oct 26, 2015 at 16:28

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

O thanks. This worked. What does the 'databases.databases' do ?
I just noticed, that somehow in your databases ($scope.databases = response) object, when you consoled it, you have 'databases' field. And only this field contains array of contacts
0

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.

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.