Reputation: 7
I am new bee in learning node js. I am trying to read a file from the same directory and then to print its contents using file system's (fs) readFile method. I see that the program is not entering into this method when I run it. I tried putting console.log() in readFile method and I see it is not printing anything inside this method.
I am not knowing exactly what is happening and why it is skipping this method.
here is the canvas-util.js file that I am running using mocha
"use strict";
var fs = require('fs'),
path = require('path');
module.exports.getEmailVariables = function(name, next) {
name = "running?";
var err = "";
var data1;
var filePath = path.join(__dirname, 'sample.txt');
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
console.log(name);
if (!err){
console.log('received data: ' + data);
data1 = data
}else{
console.log(err);
}
});
next(err, data1);
};
My test.js contains
var canvasUtil = require("../utils/canvas-util");
var assert = require("assert");
describe('Mindrill', function () {
this.timeout(30000);
it('getEmailVariables should return Email variables', function (done) {
var name="";
canvasUtil.getEmailVariables(name, function(err, results) {
assert.notEqual(null, results);
done();
});
});
});
And sample.txt contains just "hi".
checking to see if I am getting the string in file to the variable 'data1' so as to pass the test. Any suggestions would be helpful.
Upvotes: 0
Views: 1923
Reputation: 430
Instead of fs.readFile();
why not use fs.readFilesync();
? You could try the same parameters.
Upvotes: 1
Reputation: 106746
fs.readFile()
is async. Put your next(err, data1);
inside the fs.readFile()
callback.
Upvotes: 1