Jem Jem - 6 months ago 11
Node.js Question

How to call a function from a chain of module.exports in node

I've declared a function in one of my models (

review
) and exported it in the standard node way so other files can use it. I then reference it another model (
company
) and export it there as well.

Finally, I'm trying to use the function defined in
company
in another file (
companiesRoute
) but I'm getting a
company.getAllCompaniesWithReviews is not a function
error.

Is what I'm trying to do even possible?

// review.js
module.exports.getAllCompaniesWithReviews = function(next) {
// ...
// ...
// ...
};

// company.js
var reviews = require('./review');
module.exports.getAllCompaniesWithReviews = reviews.getAllCompaniesWithReviews;

// companiesRoute.js
var company = require('../models/company');
company.getAllCompaniesWithReviews(function(err, result) {
// ...
// ...
// ...
});


If I try and call the function directly from
companiesRoute
then it works:

review.getAllCompaniesWithReviews(function(err, result) {
// ...
// ...
// ...
});


This is, of course, the answer. But why doesn't my version work?

Also, node v4.4.4.

Answer

Can you please try this in company.js?

module.exports.getAllCompaniesWithReviews = function(next) { 
     reviews.getAllCompaniesWithReviews(next);
}
Comments