2017-02-28 59 views
-4

我有搜索功能,一旦我有搜索字符串从clinet我想循环通过文件和匹配来自FS中的文件的字符串,我有循环中的问题我想获得所有匹配结果并将结果发送到客户端。下面试图实现,但得到一个错误粘贴有问题。新的异步库任何帮助将不胜感激。如何使用nodejs使用异步回调?

app.js

app.get('/serverSearch', function (req, res) { 
    var searchTxt = req.query.searchTxt; 
    dirDirectory.readDirectory(function(logFiles){ 
     // res.json(logFiles); 
     if(logFiles){ 
      searchFileService.readFile(searchTxt,logFiles,function(lines,err){ 
         console.log('Logs',lines); 
          if (err) 
          return res.send(); 
          res.json(lines); 
        }) 

     } 
    }); 

    console.log('Search text', searchTxt); 
}); 

service.js

var fs = require('fs'); 
var path = require('path'); 
var async = require('async'); 
var searchStr; 
var result = []; 


//Async Method 
function readFile(str, logFiles, callback) { 
    async.series([ 
     //Load user to get `userId` first 
     function(callback) { 
      searchStr = str; 
      for (var i = 0; i < logFiles.length; i++) { 
       if (logFiles[i].filename !== '.gitignore') { 
        fs.readFile('logs/dit/' + logFiles[i].filename, 'utf8', function(err, data) { 
         if (err) { 
          return console.log(err); 
         } 
         inspectFile(data); 
        }); 
       } 
       callback(result); 
      } 
     }, 
     //Load posts (won't be called before task 1's "task callback" has been called) 
     function() { 
      function inspectFile(data, callback) { 
       var lines = data.split('\n'); // get the lines 
       lines.forEach(function(line) { // for each line in lines 
        if (line.indexOf(searchStr) != -1) { // if the line contain the searchSt 
         result.push(line); 
         // then log it 
         return line; 
        } 
       }); 
      } 
     } 
    ], function(err) { //This function gets called after the two tasks have called their "task callbacks" 
     if (err) return err; 
    }); 
}; 

错误

if (fn === null) throw new Error("Callback was already called."); 

回答

1

您应该使用async.map而不是系列。你很想知道系列做了什么,系列处理请求自上而下。您正尝试通过访问该系列中的某个功能来打破此链。这是一个不,不。

例如:

async.series([ 
    function() { 
     let i = 0; 
     do { 
      console.log("I'm first in the series: ", i); 
      i++; 
     } while (i < 3); 
     callback(); // This tells us this function has finished. 
    }, 
    function() { 
     let i = 0; 
     do { 
      console.log("I'm next in the series: ", i); 
      i++; 
     } while (i < 3); 
     callback(); // This tells us this function has finished. 
    } 
]); 

这样做的输出将是:

I'm next in the series: 0

I'm next in the series: 1

I'm next in the series: 2

直到回调进行烧制,然后告诉同步以移动到串联阵列中的下一个功能。然后

输出将是:

I'm last in the series: 0

I'm last in the series: 1

I'm last in the series: 2

在这个系列没有意义,你应该可以在当前后的系列中访问该功能。所以你永远不应该试图跨越那个。

使用async.map您实际上可以对数组中的每个实体执行操作,这基本上就是您正在尝试执行的操作。

var results = []; 
async.map(logFiles, function(logfile, callback) { 
    if (logfile.filename !== '.gitignore') { 
     fs.readFile('logs/dit/' + logfile.filename, 'utf8', function(err, data) { 
      if (err) { 
       callback(err, null); 
      } 
      var lines = data.split('\n'); // get the lines 
      lines.forEach(function(line) { // for each line in lines 
       if (line.indexOf(searchStr) != -1) { // if the line contain the searchSt 
        results.push(line); 
        callback(null, results); 
       } 
      }); 
    } 
}), function(error, result) { 
    results.map(result => { 
     console.log(result); 
    }); 
}); 

此外,你应该使用util.inspect而不是console.log,它更清洁,有更多的选择。

关于此的文档有点粗糙,但在这里。 https://caolan.github.io/async/docs.html#map希望这有助于!

+0

有在你的代码中的一些语法问题,我试图解决 – hussain

+0

啊对不起,我是混合ES6和ES5但它应该是接近你所需要的。希望能帮助到你! – djowinz

+0

我是否必须从'readFile'功能以及 – hussain

0

你应该使用async.eachSeries method

function readFile(str, logFiles, callback) { 
    async.eachSeries(array, function(item, cb){ 
     //Process item 
     cb(error,item); 
    }, function(err){ 
     if (err) { 
      console.log("Some error in one of the items"); 
      callback(err); 
     } else { 
      console.log("All arrays items have been treated successfully"); 
      callback(null); 
     } 
    }); 
} 

而我会建议加载用户帖子之前使用async.eachSeries函数。

+0

你的代码工作,其打印所有阵列成功的治疗,但其不是现在@hussain什么结果发送到客户端 – hussain

+0

?我已经更新了答案。我不知道你还需要解决客户端的回调。 – daniegarcia254