Javascript 将js函数导入到node.js中的module.exports
我有一个Javascript 将js函数导入到node.js中的module.exports,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我有一个模块。导出文件,在其中定义一组函数。 通常,我在其中放置的函数是对mongodb数据库的查询,我在其中异步执行记录中的多个操作 除此之外,我还有一个helpers文件,其中包含一些函数,这些函数在我的不同模块的几段代码中使用 我希望能够在我的模块中使用这些助手函数。导出:' var helpers = require('./helpers'); module.exports = { process: function(callback){ var curso
模块。导出
文件,在其中定义一组函数。
通常,我在其中放置的函数是对mongodb数据库的查询,我在其中异步执行记录中的多个操作
除此之外,我还有一个helpers文件,其中包含一些函数,这些函数在我的不同模块的几段代码中使用
我希望能够在我的模块中使用这些助手函数。导出
:'
var helpers = require('./helpers');
module.exports = {
process: function(callback){
var cursor = Col1.find().lean().cursor();
cursor.on('data', function(doc) {
console.log(helpers.function1(doc));
});
cursor.on('close', function() {
// Called when done
callback();
});
}
};
我得到以下行console.log(helpers.function1(doc))的错误代码>:
TypeError:helpers.function1不是函数
helpers.js:
function function1(record){
return '';
};
已编辑:您需要导出helpers.js中的函数1添加:
module.exports = {function1};
并且还替换
var helpers = ('./helpers');
到
var helpers=('./helpers')代码>??单独:如果helpers.js
如图所示,它不会导出任何内容。@t.J.Crowder输入我使用的代码需要。要按照OP使用它的方式使用,它应该是module.exports.function1=function1代码>,而不是module.exports=function1
@T.J.Crowder您的答案是正确的,请发布,我会接受的,谢谢。
var helpers = require('./helpers');