Export a function with Node JS

Let's look at your code:

module.exports = {
    ...
};

module.exports = function(owner) {
    ...
}

You're assigning two different things to the same property. The second assignment overrides the first one.


To do what you're asking, you need to export the function first and then add the properties separately:

module.exports = function(owner) {
    ...
}

module.exports.User = user;
module.exports.Note = note;

This is standard in node


Instead of defining note and user with var, use exports.note and exports.user

 exports.user = bookshelf.Model.extend({
    tableName : 'tblUsers',
    idAttribute : 'email'
 });

 exports.note = bookshelf.Model.extend({
    tableName : 'tblNotes',
    idAttribute : 'noteId'
 });

Then you'll more likely than not have to set your function export to a named function. (or make it a class?)

 exports.MyClass = function(owner) {
   return knex('note').where('public',1).orWhere({ownerName : owner}).select('noteId');
 }

If you're worried about ease of imports, you can assign MyClass to a variable, instead of the whole package

 var MyClass=require('module').MyClass;

Tags:

Node.Js