2015-04-06 28 views
1

在node.js中使用async時出現綁定錯誤。有問題的代碼:使用async.parallel時出錯

var async = require('async'); 
var fs = require('fs'); 
var path = require('path'); 

function ignoreWhiteSpaceJudge(outDesired, outGenerated){ 

var contentOutDesired = ""; 
var contentOutGenerated = ""; 

async.parallel([ 

    function(outDesired, callback) { 
      console.log(outDesired); 

      fs.readFile(outDesired, 'utf8',function(error, data) { 
       if (error) {    
         return callback(error); 
       } else { 
         contentOutDesired = data; 
         return callback(); 
       }  
      }); 
    }, 

    function(outGenerated, callback) { 

      fs.readFile(outGenerated, 'utf8', function(error, data) { 
       if (error) {    
         return callback(error); 
       } else { 
         ontentOutGenerated = data; 
         return callback(); 
       }  
      }); 
    }], 

    function(error){ 
     if(error){ 
      console.log(error); 
     } 
     else{ 
      console.log(contentOutDesired); 
      console.log(ontentOutGenerated); 
     } 

    }); 
} 


var pathToOutDesired = path.normalize('/home/repos/gabbar/testcases/outputs/output_1_1.out'); 
var pathToOutGenerated = path.normalize('/home/repos/gabbar/testcases/outputs/output_1_2.out'); 

ignoreWhiteSpaceJudge(pathToOutDesired, pathToOutGenerated); 

的錯誤我得到這個樣子的:

[Function] 

fs.js:423 
    binding.open(pathModule._makeLong(path), 
      ^
TypeError: path must be a string 
     at Object.fs.open (fs.js:423:11) 
     at Object.fs.readFile (fs.js:206:6) 
     at async.parallel.fs.readFile.ontentOutGenerated   (/home/repos/gabbar/validation/ignoreWhiteSpaceJudge.js:17:18) 
at /home/repos/gabbar/node_modules/async/lib/async.js:570:21 
at /home/repos/gabbar/node_modules/async/lib/async.js:249:17 
at /home/repos/gabbar/node_modules/async/lib/async.js:125:13 
at Array.forEach (native) 
at _each (/home/repos/gabbar/node_modules/async/lib/async.js:46:24) 
at async.each (/home/repos/gabbar/node_modules/async/lib/async.js:124:9) 
at _asyncMap (/home/repos/gabbar/node_modules/async/lib/async.js:248:13) 

我是比較新的Node.js的,並試圖使用async模塊首次。在這方面有人能幫助我嗎?

回答

1

您正在使用parallel的回調函數覆蓋您的路徑。

只是刪除從函數的第一個參數是回調,而不是你的數據:

function(callback) { 
     console.log(outDesired); 

     fs.readFile(outDesired, 'utf8',function(error, data) { 
      if (error) {    
        return callback(error); 
      } else { 
        contentOutDesired = data; 
        return callback(); 
      }  
     }); 
}, 

function(callback) { 

     fs.readFile(outGenerated, 'utf8', function(error, data) { 
      if (error) {    
        return callback(error); 
      } else { 
        ontentOutGenerated = data; 
        return callback(); 
      }  
     }); 
} 
+0

感謝幫助。 – Quixotic 2015-04-06 09:01:27