I think he already is exporting the constructor, else the last 3 lines
would just fail.
Ricks advise is ok and will work WHEN the result of new DBManager isn't
used immediately
There are async calls, 3 of them, so the constructed object is not doen
after construction, but later. this is bad, because the semantics of a
constructor is a) synchronous and b) the constructed object is done and
ready after constructor has returned. In my opinion it is better to NOT to
do any async stuf in a constructor. I'd prefer one of this 2 options:
constructor+init method or async factory function:
1)
function DBManager(port, host, dbname) {
this.port = port;
this.host = host;
this.dbname = dbname;
}
DBManager.prototype.init = function(doneCB){
// Retrieve
var MongoClient = require('mongodb').MongoClient;
var that = this;
// Connect to the db
MongoClient.connect("mongodb://localhost:" + port + "/" + dbname, function(
err, db) {
// do all the other async stuf and assign
that.asyncCreated = asyncResult;
// when all async stuf is done
doneCB();
});
};
2)
module.exports = function createDBManager(port, host, dbname, callback) {
var dbManager = {};
// Retrieve
var MongoClient = require('mongodb').MongoClient;
function done(){
if( dbManager.db && dbManager.domainStore &&
dbManager.emailStore) callback(null, dbManager);
}
// Connect to the db
MongoClient.connect("mongodb://localhost:" + port + "/" + dbname, function(
err, db) {
if (!err) {
console.log("We are connected");
dbManager.db = db;
dbManager.domainStore = db.collection('domain', function(err,
result) {
if (err) {
console.log("unable to get domain store " + err);
callback(err);
} else done();
});
dbManager.emailStore = db.collection('email',
function(err, result) {
if (err) {
console.log("unable to get email store " + err);
callback(err);
} else done();
});
}else{ callback(err); }
});
};
Am Dienstag, 12. März 2013 05:38:14 UTC+1 schrieb Wil Moore:
>
> > var z = require('./smailer.js')
>> > var k = new z.DBManager(27017, 'localhost', 'dev')
>> > k
>> {}
>
>
>
> Put the following into your module:
>
> module.exports.DBManager = DBManager;
>
>
> As you have it currently, your module exports nothing so you have access
> to nothing.
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.