简体   繁体   English

来自外部必需的javascript文件的nodejs异步瀑布使用功能

[英]nodejs async waterfall use function from external required javascript file

I am fairly new to nodejs, and to keep my code brief, was hoping I could use functions from a .js file I include in my async waterfall list of functions. 我对Node.js相当陌生,为了使我的代码简短,希望我可以使用异步瀑布式函数列表中包含的.js文件中的函数。 I have tried to include one function, and am getting either a 'TypeError: callback is not a function' exception or 'Cannot read property 'Symbol(Symbol.toStringTag)' of undefined'. 我试图包括一个函数,并得到“ TypeError:回调不是函数”异常或“无法读取未定义的属性'Symbol(Symbol.toStringTag)'”。 I have searched and searched and cannot find an answer, but hoping someone can give me the best way, as with callbacks and functions I'm not sure what the best solution is. 我已经搜索了很多,找不到答案,但是希望有人能给我最好的方法,就像回调和函数一样,我不确定最好的解决方案是什么。

Am hoping my code will be like this: 希望我的代码是这样的:

var module1 = require('./lib/module1.js'),
   module2 = require('./lib/module2.js');

async.waterfall([
   module1.externalfunc1,
   module2.externalfunc2
], function (err, result) {
if (err) {
    console.log('ERROR: ');
    console.log(err);
} else {
    console.log('Completed streaming1');
    console.log(result);
}

My guess is I should put the waterfall functions inside another function, like function(){module1.externalfunc;},function(){module2... 我的猜测是我应该将瀑布形函数放在另一个函数中,例如function(){module1.externalfunc;},function(){module2 ...

Think I am just not understanding callbacks well enough, as I'm not sure if the external function will know what "callback" is since its outside of the code with waterfall. 认为我只是不太了解回调,因为我不确定外部函数是否知道什么是“回调”,因为它在带有瀑布的代码外部。 But I have to believe this is possible. 但是我必须相信这是可能的。

Thanks for any help. 谢谢你的帮助。

meant to say, i have externalfunc1 created with a function defined, which i export, so in the file have: 意思是说,我使用定义的函数创建了externalfunc1,然后导出该函数,因此在文件中具有:

function externalfunc1(param1, callback){...} 
module.exports = externalfunc1;

The problem that you are facing is that you are exporting the function externalfunc1 as your entire module. 您面临的问题是要将函数externalfunc1导出为整个模块。 Ie

var module1 = require('./lib/module1.js')
module1  === externalfunc1

Either change the export to : 可以将导出更改为:

module.exports = { externalfunc1 }

or import as : 或导入为:

var externalfunc1 = require('./lib/module1.js')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM