Reputation: 1406
I want to initialize the content variable in readFile() and want to use this variable in other methods of nodejs. Inside the readFile() content variable is initialized, but outside the scope of this variable is not existing.
fs = require('fs');
var flag=0;
var content="";
fs.readFile('./file.txt', function (err, data) {
if (err) {
throw err;
}
console.log("file reading complete");
content=data;
console.log("data:"+content);
});
if(content=="")
console.log("value not set");
After compiling this code, i found that the content variable is initialized in readFile(), but it doesn't maintain this value upto end of the code. I want to maintain the scope of this content variable throughout the program, how can i do this task.
I have also tried the callback method style, but using the callback method style is not working in case of running the exec like this,
exec(cmd,function(error,stdout,stderr){
});
Below is the piece of code,
var child = require('child_process');
var terminal = child.exec("java Temp");
var output="";
process.stdin.pipe(terminal.stdin);
terminal.stdin.on("end", function() {
// process.exit(0);
});
terminal.stdout.on('data', function (data) {
output+=data;
// console.log(''+data); this line prints a=10 b=20
});
terminal.stderr.on('data', function (data) {
output+=data;
});
terminal.stdin.write('10 20');
console.log(""+output); // this line doesn't print any thing.
the source of compiled file Temp is
import java.io.*;
import java.util.Scanner;
class Temp
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
System.out.println("a="+a+" b="+b);
}
}
how can i use the callback method, to work out this problem.
Upvotes: 1
Views: 1309
Reputation: 1047
Since Node.js is asynchronous, the last if condition is performed, while the readFile() operation is running and content is still empty. You will need to add a callback, in order to check the content value once the readFile() operation is done.
You should be able to get the value of the content variable anywhere in your program, but it will be empty until readFile() finished.
fs = require('fs');
var flag=0;
var content="";
fs.readFile('./file.txt', function (err, data) {
if (err) {
throw err;
}
console.log("file reading complete");
content=data;
check(); // CALLBACK HERE
console.log("data:"+content);
});
function check() {
if(content=="")
console.log("value not set");
}
}
UPDATE
Now that you have posted more code:
I do not understand what it your code does exactly, but you are doing the same mistake as in the initial question. The console.log line at the and is executed, way before any value was ever appended to the output variable.
If you output the output variable here
terminal.stdin.on("end", function() {
console.log(''+output); // Output here
// process.exit(0);
});
then you should get the desired output.
Have a look at the following article, which is explaining the asynchronous model in Node.js. Its essential to understand this in order to write proper applications. http://blog.shinetech.com/2011/08/26/asynchronous-code-design-with-node-js/
Upvotes: 4
Reputation: 2089
You can also use a sync version of fs.readFile:
var content = fs.readFileSync("filepath");
Upvotes: 1