我已经在我的一个模型(review)中声明了一个函数,并以标准节点方式导出了该函数,以便其他文件可以使用它。然后,我引用另一个模型(company)并将其导出到那里。

最后,我试图在另一个文件(company)中使用companiesRoute中定义的函数,但出现了company.getAllCompaniesWithReviews is not a function错误。

我要做什么甚至有可能吗?

// 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) {
  // ...
  // ...
  // ...
});


如果我尝试直接从companiesRoute调用该函数,那么它将起作用:

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


当然,这就是答案。但是为什么我的版本不起作用?

另外,节点v4.4.4。

最佳答案

您可以在company.js中尝试吗?

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

关于javascript - 如何从 Node 中的module.exports链中调用函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37326024/

10-12 17:37
查看更多