2016-09-22 4 views
1

async.waterfallを使用するとき、モジュール間で変数を渡すときに問題があります。これが私のメインランナーです。ノードasync.waterfallモジュール間での変数の受け渡し

var async = require('async'); 
var helpers = require('./1-helpers'); 

var file = process.argv[2]; 
var body = ''; 

async.waterfall([helpers.getURL, helpers.openURL, helpers.printBody]); 

と機能これは私が取得していますエラーですすべてこのファイルに

var fs = require('fs'); 
var http = require('http'); 

module.exports = { 

    getURL: function (done) { 
    fs.readFile(file, 'utf8', this.gotFileData); 
    }, 

    gotFileData: function (err, url) { 
    if (err) return done(err); 
    done(null, url); 
    }, 

    openURL: function (url, done) { 
    http.get(url, this.handleResponse).on('error', function (e) { 
     done(e); 
    }); 
    }, 

    handleResponse: function (res) { 
    res.on('data', this.readChunk); 
    res.on('end', function gotBody (chunk) { 
     done(null, body); 
    }); 
    }, 

    readChunk: function (chunk) { 
    body += chunk.toString(); 
    }, 

    printBody: function (err, body) { 
    if (err) return console.error(err); 
    console.log(body); 
    } 

} 

です:

/Users/wimo/dev/nodejs/async-you/1-helpers.js:7 
    fs.readFile(file, 'utf8', this.gotFileData); 
      ^

ReferenceError: file is not defined 
    at getURL (/Users/wimo/dev/nodejs/async-you/1-helpers.js:7:17) 
    at nextTask (/Users/wimo/dev/nodejs/async-you/node_modules/async/dist/async.js:5021:18) 
    at Object.waterfall (/Users/wimo/dev/nodejs/async-you/node_modules/async/dist/async.js:5024:9) 
    at Object.<anonymous> (/Users/wimo/dev/nodejs/async-you/1.js:7:7) 
    at Module._compile (module.js:556:32) 
    at Object.Module._extensions..js (module.js:565:10) 
    at Module.load (module.js:473:32) 
    at tryModuleLoad (module.js:432:12) 
    at Function.Module._load (module.js:424:3) 
    at Module.runMain (module.js:590:10) 

を通じて「ファイル」と「ボディ」の変数を渡すことで任意のヒントasync.waterfall機能はどうですか?

多くのおかげで、 ヴィム

答えて

0

main.jsで

はasync.waterfallこの中

峠最初の関数async.constantはのgetURL関数の引数として変数ファイルを渡します。

var fs = require('fs'); 
var http = require('http'); 
module.exports = { 
getURL: function (file, callback) { 
    fs.readFile(file, 'utf8', function (err, fileData) { 
    if (err) { 
    callback(err); 
    return; 
    } 
    callback(null, fileData); 
    }); 
}, 
openURL: function (url, callback) { 
    http.get(url, function (err, httpResponse) { 
    if (err) { 
    callback(err); 
    return; 
    } 
    callback(null, httpResponse); 
    }); 
}, 
printBody: function (httpResponse, callback) { 
    httpResponse.on('data', function (chunk) { 
    console.log(chunk); 
    callback(null, 'done'); 
}); 
} 
} 

Async.jsは、エラーが発生したかどうかと、コールバックへの引数は、次のタスクに渡されたかどうかを決定するためにコールバックを使用していますhelper.jsでDocumentation of async.constantHow to pass argument to first waterfall function

async.waterfall([async.constant(file), helpers.getURL, helpers.openURL, helpers.printBody], function(err) 
{ console.log(err); }); 

を参照してください。 ここで、fileData、fs.readile関数の結果がgetURLに渡されます(次のタスク)。したがって、urlはfileDataの値を取得します。 async.waterfall documentation

+0

すごい、ありがとう!代わりにasync.applyを使用し、 "file" varを渡してもうまく動作します。 –

関連する問題