I have this code below. Using the readline and fs module I am trying to search for a particular word in a file line by line and if that word exist push the contents of that word to an array but when I try to return this array I get an undefined. Any ideas?
var fs = require('fs');
var readline = require('readline');
var reader = readline.createInterface({
input: fs.createReadStream('MY_FILE_NAME.txt'),
});
function newDefineWord(reader1) {
this.reader = reader1;
}
newDefineWord.prototype.define = function(searchTerm) {
var arr = [];
this.reader.on('line', function (line) {
if (line.search(searchTerm)!== -1) {
arr.push(line);
console.log(arr);
}
});
return arr;
}
var word = new newDefineWord(reader);
console.log(word.define('libro'));
JSworks which seems like thereturn arris being retuened before the function gets executed, it's async, right? Check this.