简体   繁体   中英

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. 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'. 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...

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:

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. 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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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