Reputation: 144
This is my first project in nodejs. I am unable to find the cause :
fs.readFile("/home/shaurya/Desktop/test.txt","utf-8",
function(err,filedata1){
fs.readFile(filedata1,"utf-8",function(err,filedata2){
console.log(filedata1);
console.log(filedata2);
});
});
"/home/shaurya/Desktop/test.txt" contains the location of a file as a string. I am reading this test.txt in outer readFile call and passing the file content as parameter to inner readFile.
Content of test.txt is : /home/shaurya/Desktop/Parser.hs.
I was expecting that I will get the output as a string for console.log(filedata2) call.
Instead I got undefined
.
Any thoughts?
Upvotes: 0
Views: 1095
Reputation: 2244
I had a similar problem. In order to solve this you would you would need to append a .trim() method to the filedata1
. Apparently sometimes text editors put an extra space or new line character after the end of the stream. This should solve your problem.
Your new code:
fs.readFile("/home/shaurya/Desktop/test.txt","utf-8",
function(err,filedata1){
fs.readFile(filedata1.trim(),"utf-8",function(err,filedata2){
console.log(filedata1);
console.log(filedata2);
});
});
// Please note the .trim() after filedata1
Upvotes: 1