[英]How to use the 'this' reference in fs.readFile in Node.js
[英]Node.js using async - How can I properly use async.forEachLimit with fs.readFile?
我正在开发Node.js应用程序的一部分,该应用程序需要从各个文件中的特定点获取版本信息。 我已经使用npm包async对此功能进行了编码。
我确切地知道我的问题是什么。 但是,由于我对Node.js并不陌生,甚至对异步包也不陌生,所以我没有实现正确的东西。
看来version变量的内容没有及时响应我的回答。 换句话说,响应是在版本可以响应之前发送的。
下面是相关代码:
exports.getAllVersionInformation = function(request, response) {
if (!utilities.checkLogin(request, response))
return;
// Get the array of file information stored in the config.js file
var fileCollection = config.versionsArray;
// Declare the array to be used for the response
var responseObjects = [];
async.forEachLimit(fileCollection, 1, function(fileInformation, taskDone) {
// Declare an object to be used within the response array
var responseObject = new Object();
// Retrieve all information particular to the given file
var name = fileInformation[0];
var fullPath = fileInformation[1];
var lineNumber = fileInformation[2];
var startIndex = fileInformation[3];
var endIndex = fileInformation[4];
// Get the version number in the file
var version = getVersionInFile(fullPath, lineNumber, startIndex,
endIndex, taskDone);
console.log('Ran getVersionInFile()');
// Set the name and version into an object
responseObject.name = name;
responseObject.version = version;
// Put the object into the response array
responseObjects.push(responseObject);
console.log('Pushed an object onto the array');
}, function(error) {
console.log('Entered the final');
if (error == null)
// Respond with the JSON representation of the response array
response.json(responseObjects);
else
console.log('There was an error: ' + error);
});
};
function getVersionInFile(fullPath, lineNumber, startIndex, endIndex, taskDone) {
console.log('Entered getVersionInFile()');
var version = fs.readFile(fullPath,
function(error, file) {
if (error == null) {
console.log('Reading file...');
var lineArray = file.toString().split('\n');
version = lineArray[lineNumber].substring(startIndex,
endIndex + 1);
console.log('The file was read and the version was set');
taskDone();
} else {
console.log('There was a problem with the file: ' + error);
version = null;
taskDone();
}
});
console.log('Called taskDone(), returning...');
return version;
};
我试着玩getVersionInFile函数如何返回数据。 我已经移动了taskDone()函数,以查看是否会有所作为。 我问过Google很多关于异步以及在我的上下文中使用异步的问题。 我似乎无法正常工作。
我使用的一些更重要的资源是: http : //www.sebastianseilund.com/nodejs-async-in-practice http://book.mixu.net/node/ch7.html
我添加了console.log语句来跟踪代码流。 这是图片:
此外,我有部分预期的回应。 也是这样:![浏览器输出] http://imgur.com/rKFq83y
此输出的问题在于,JSON中的每个对象也应具有一个版本值。 因此,JSON应该类似于:[{“ name”:“ WebSphere”,“ version”:“ xxxx”},{“ name”:“ Cognos”,“ version”:“ xxxx”}]
如何获取我的getVersionInFile()函数以及时正确地给我版本号? 另外,如何确保不做任何阻塞而异步执行此操作(这是使用异步进行流控制的原因)?
任何见解或建议,将不胜感激。
一个问题是getVersionInFile()
在异步readFile()
完成之前正在返回一个值(也是异步的, readFile()
不会返回有意义的值)。 另外,为forEachLimit()
使用限制/并发性为1与forEachSeries()
相同。 这是一个使用mapSeries()
的示例,该示例应为您提供相同的最终结果:
exports.getAllVersionInformation = function(request, response) {
if (!utilities.checkLogin(request, response))
return;
// Get the array of file information stored in the config.js file
var fileCollection = config.versionsArray;
async.mapSeries(fileCollection, function(fileInformation, callback) {
// Retrieve all information particular to the given file
var name = fileInformation[0];
var fullPath = fileInformation[1];
var lineNumber = fileInformation[2];
var startIndex = fileInformation[3];
var endIndex = fileInformation[4];
// Get the version number in the file
getVersionInFile(fullPath,
lineNumber,
startIndex,
endIndex,
function(error, version) {
if (error)
return callback(error);
callback(null, { name: name, version: version });
});
}, function(error, responseObjects) {
if (error)
return console.log('There was an error: ' + error);
// Respond with the JSON representation of the response array
response.json(responseObjects);
});
};
function getVersionInFile(fullPath, lineNumber, startIndex, endIndex, callback) {
fs.readFile(fullPath,
{ encoding: 'utf8' },
function(error, file) {
if (error)
return callback(error);
var lineArray = file.split('\n');
version = lineArray[lineNumber].substring(startIndex,
endIndex + 1);
callback(null, version);
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.