我对JavaScript比较陌生,遇到了一个问题,我试图从一个文件中导出一个函数来调用该函数。如何调用此功能?重要的是,我能够从该文件中调用此函数并将其导出以用于其他用途。非常感谢您的帮助!
我在下面添加了相关代码:
module.exports.handleCreatedTs = function(criteria, newSearch){
...Lots of Calculations happen here...
}
module.exports.handleDateAndTermsDueDate = function(criteria, newSearch, isTermsDueDate){
var tempSearchObj = {};
handleCreatedTs(criteria, tempSearchObj); //This is where the exception is thrown
if(isTermsDueDate){
newSearch.termsDueDate = tempSearchObj;
}
else{
newSearch.date = tempSearchObj;
}
}
module.exports.handleInvoiceSupplierName = function(criteria, newSearch){
if(criteria.operator != "equals"){
return; //Not going to handle this
}
newSearch.supplierOrg.push({text: criteria.value, value: criteria.value});
}
答案 0 :(得分:1)
您的代码handleCreatedTs()
中没有变量。您已将一个匿名函数分配给module.exports.handleCreatedTs()
。
您可以 用module.exports.handleCreatedTs()
来调用它,但是如果您打算从模块中调用它,可能更干净的方法是先定义命名函数,然后将其添加到module.exports
中。
function handleCreatedTs(criteria, newSearch){
// ...Lots of Calculations happen here...
}
// you can call handleCreatedTs()
// export the function:
module.exports.handleCreatedTs = handleCreatedTs