javascript - I want to return the contents of file using node js -


i have written following code in protractor.

helper.js:

var fs = require('fs'); helper = function(){     this.blnreturn = function(){         var filepath = '../protractor_pgobjmodel/results/dontdelete.txt';         fs.readfilesync(filepath, {encoding: 'utf-8'}, function (err, data){             if (!err) {                 console.log(data);                 return data;             } else {                return "false";             }         });     }; }; module.exports = new helper(); 

------------actual file above js being called-------------------

describe("read contents of file",function(){   var helper = require("../genericutilities/helper.js");   it('to test read data',function(){     console.log("helper test - " + helper.blnreturn());      }); }); 

-------output-------------

helper test - undefined 

any in regard appreciated blocking work.

you confusing between reading file synchronously(readfilesync) , async(readfile).

you trying read file synchronously, using callback parameter, right way either

return fs.readfilesync(filepath, {encoding: 'utf-8'}); 

or

this.blnreturn = function(cb){     ...     fs.readfilesync(filepath, {encoding: 'utf-8'}, function (err, data){         if (!err) {             console.log(data);             cb(data);         } else {            cb("false");         }     }); 

also, on unrelated note, var keyword missing in helper definition, helper.js can reduced to:

var fs = require('fs'); function helper(){}  helper.prototype.blnreturn = function(){     var filepath = '../request.js';     return fs.readfilesync(filepath, {encoding: 'utf-8'}); };  module.exports = new helper(); 

Comments