Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export a function with Node JS

Tags:

node.js

I need help with function export in Node.Js

Here is my module.js

var knex = require('knex')({
    client : 'mysql',
    connection : {
        host : 'localhost',
        user : 'root',
        password : '123',
        database : 'dbNode'
    }
}),bookshelf = require('bookshelf')(knex);

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

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


module.exports = {
   User : user,
   Note : note,

};

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

I have to export both the function and the bookshelf models (User and Note) but when I try to use the Model.User it returns me the error: Model.User is not a function

like image 492
Ivan Bertola Avatar asked Jun 03 '16 11:06

Ivan Bertola


2 Answers

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.

Better yet, don’t assign the function directly to module.exports but add it as a property to it:

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

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

This way you have real separate exports instead of an exported function that strangely also has User and Note properties.

like image 75
fregante Avatar answered Oct 19 '22 14:10

fregante


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;
like image 44
Valkyrie Avatar answered Oct 19 '22 13:10

Valkyrie