Saturday, 6 August 2016

javascript - How to get variable from exec function in Node.js

I need to use the value of the variable w1 and h1 outside the exec function, in console.log.



exec(command, function(err, stdout, stderr) {

var resolution = stdout.split("x");
w1 = resolution[0];
h1 = resolution[1];


});

console.log(w1 + " - " + h1);


The console.log displays the correct values ​​of the variables, but before displays this list of errors:



ReferenceError: w1 is not defined
at app.js:30:21

at callbacks (app/node_modules/express/lib/router/index.js:164:37)
at param (app/node_modules/express/lib/router/index.js:138:11)
at pass (app/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (app/node_modules/express/lib/router/index.js:173:5)
at Object.router (app/node_modules/express/lib/router/index.js:33:10)
at next (app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.expressInit [as handle] (app/node_modules/express/lib/middleware.js:30:5)
at next (app/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.query [as handle] (app/node_modules/express/node_modules/connect/lib/middleware/query.js:44:5)



I found this similar question, but don't work for me.
How can we access variable from callback function in node.js?



Thank you.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...