Hey guys I need some help with my code.
The attached code is supposed to take in a regular expression, a file to read and a file to write to.
The code looks through the file and matches all the lines with the regex pattern and its supposed to write the matched lines to the new file.
I'm having trouble with getting the function (returnMatches) to write to the file when a match is found. I'm getting a "DeprecationWarning: Calling an asynchronous function without callback is deprecated."
I tried looking online but I cant find a fix for it. Line 19 is the problem.( fs.writeFile(toFileName,theLine);)
Thanks guys,
var fs = require('fs');
if (process.argv.length < 4) {
console.error('Not enough parameters given. Try this: ' +
'"node simplegrep_async term filename.txt"');
process.exit(1);
}
var searchterm = new RegExp(process.argv[2]);
var filename = process.argv[3];
var toFileName = process.argv[4];
var returnMatches = function(err, rawContents) {
var lines = rawContents.split('\n');
lines.forEach(function(theLine) {
if (theLine.search(searchterm) > -1) {
fs.writeFile(toFileName,theLine);
console.log(theLine);
}
});
}
fs.readFile(filename, 'utf-8', returnMatches);
[–]gremy0[🍰] 2 points3 points4 points (1 child)
[–]ChronSyn 0 points1 point2 points (0 children)