-2

I have this code

var mongodb = require('mongodb'),  assert = require('assert');

var Db = mongodb.Db;

var db = new Db('local', new Server('localhost', 27017);

db.open(functioin(err, db){
    if(err) throw err;
    var adminDb = db.admin();
    adminDb.listDatabases(function(err, dbs){
        if(err) throw err;
        console.log(dbs);
    });
});

I want to export the dbs variable from the callback of the listDatabases function. Is there any way?

captainsac
  • 2,484
  • 3
  • 27
  • 48
Samson Ayalew
  • 359
  • 3
  • 8
  • 1
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Ben Fortune May 27 '15 at 11:53

1 Answers1

0

Thanks guys. It finally worked for me with this code.

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
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);

    db.close();
  });
});
Samson Ayalew
  • 359
  • 3
  • 8