node.js请求中的多个函数

我试图从单个请求中的数据,像在代码波纹一样,但它不起作用。 当我尝试一个程序,它的工作。 如何在一个请求过程中调用多个过程?

var fs = require('fs'); var request = require('request'); var cheerio = require('cheerio'); var link = "www.google.com"; request(link, function (error, response, html) { if (!error && response.statusCode == 200) { var $ = cheerio.load(html); //scrape class $('.someclass').filter(function () { var data = $(this); var description = data.html(); //write data to file fs.appendFile('description.txt', description + "\n", function (err) { if (err) throw err; }); }); //scrape class1 $('.someclass1').filter(function () { var data = $(this); var description1 = data.html(); //write data to file fs.appendFile('description1.txt', description1 + "\n", function (err) { if (err) throw err; //console.log('The "description" was appended to file!'); }); }); //scrape class2 $('.someclass2').filter(function () { var data = $(this); var description2 = data.html(); //write data to file fs.appendFile('description2.txt', description2 + "\n", function (err) { if (err) throw err; //console.log('The "description" was appended to file!'); }); }); } }); 

       

网上收集的解决方案 "node.js请求中的多个函数"

filter没有做你认为的事情。 您正在寻找.each()。 filter获取一个列表并返回一个较小的列表。 每个迭代项目。

 function writeToFile($, methodStr, fileName, modifyFunc) { return function () { // Whoever calls this function gets its innerhtml written to whatever // fileName is passed to the outer function. var text = $(this)[methodStr]() + "\n"; if (typeof modifyfunc === 'function') { text = modifyFunc(text); } fs.appendFileSync(fileName, text); }; } 

然后像这样应用它

 request(link, function (error, response, html) { if (!error && response.statusCode == 200) { var $ = cheerio.load(html); // these each statements say that for every element that has .someclass // give it the inner function in writeToFile where fileName is description.txt $('.someclass').each(writeToFile($, 'text', 'description.txt')); $('.someclass1').each(writeToFile($, 'html', 'description1.txt')); $('.someclass2').each(writeToFile($, 'text', 'description.txt2', function (str){ return str + "Here is a change that will also get written to the file"; })); } }