understanding module.export

74 views
Skip to first unread message

Ken Koontz

unread,
Nov 14, 2012, 8:41:32 PM11/14/12
to nod...@googlegroups.com
I am working on an express app. I've separated my route logic outside of the app.js file, which I'm using for config. I'm trying to configure my db in app.js and export this into any route module that needs it.

Here is my db code that i'm pulling into app.js.

db file snippet

app.js file snippet

The output from console.log(db); in the app.js file appears to be an empty object. However, when I reference a function such as console.log(db.create) it returns [ Function ]. Why does console.log(db) return {} and not something like {create: [ Function ].....}

Alex Kocharin

unread,
Nov 14, 2012, 8:44:49 PM11/14/12
to nod...@googlegroups.com
 
console.log hides object's prototype
--
// alex
 
 
15.11.2012, 05:41, "Ken Koontz" <kenneth...@gmail.com>:
--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Ken Koontz

unread,
Nov 14, 2012, 8:46:27 PM11/14/12
to nod...@googlegroups.com, al...@kocharin.ru
Thanks! How do I reveal it?

Forrest L Norvell

unread,
Nov 14, 2012, 8:53:50 PM11/14/12
to nod...@googlegroups.com, al...@kocharin.ru
On Wednesday, November 14, 2012 at 5:46 PM, Ken Koontz wrote:
Thanks! How do I reveal it?
Either via util.inspect() or console.error("db: %j", db).


Ken Koontz

unread,
Nov 14, 2012, 9:46:11 PM11/14/12
to nod...@googlegroups.com, al...@kocharin.ru
Thanks for the help. I'm now having problems with the export. In app.js I export db. In the other module I attempt to use it.

https://gist.github.com/4076308

When I require db the variable is undefined. Any suggestions?


Forrest L Norvell

unread,
Nov 14, 2012, 9:53:06 PM11/14/12
to nod...@googlegroups.com
On Wednesday, November 14, 2012 at 6:46 PM, Ken Koontz wrote:
Thanks for the help. I'm now having problems with the export. In app.js I export db. In the other module I attempt to use it.

https://gist.github.com/4076308

When I require db the variable is undefined. Any suggestions?
Modules are loaded synchronously but I'm guessing your database setup is asynchronous (i.e. done using a callback). You should rewrite the app module in such a way that you're exporting a function that takes a callback, and that callback gets called with the db object as a parameter once it's available. You could also take a look at using something like Cloud9's architect or flatiron's broadway to help manage dependencies by treating them like plugins.

Reply all
Reply to author
Forward
0 new messages