node.js - How to capture the 'code' value into a variable? -


i'm using below code reset password on linux box using nodejs , ssh2 module:

// file * ./workflow/ssh.js  var client = require('ssh2').client; var conn = new client();  // var opts = require('optimist') //     .options({ //         user: { //             demand: true, //             alias: 'u' //         }, //     }).boolean('allow_discovery').argv;  // definition of reset password;  var resetpassword = function(user, host, loginuser, loginpassword){  var command = 'echo -e "linuxpassword\nlinuxpassword" | passwd '+ user;  conn.on('ready', function() {   console.log('client :: ready');   conn.exec(command, function(err, stream) {     if (err) throw err;     stream.on('close', function(code, signal) {       console.log('stream :: close :: code: ' + code + ', signal: ' + signal);       conn.end();       return(code);     }).on('data', function(data) {       console.log('stdout: ' + data);     }).stderr.on('data', function(data) {       console.log('stdlog: ' + data);     });   }); }).connect({   host: host,   port: 22,   username: loginuser,   password: loginpassword }); };  exports.resetpassword = resetpassword; 

i'm calling resetpassword password function other module , test.js below.

var ssh = require('./workflow/ssh.js');  result = ssh.resetpassword('test122', '192.168.0.101', 'root' , 'password'); console.log(result) 

but console.log says "undefined". tried using process.nexttick, no luck. please help.

welcome world of developing asynchronous operations in node.js. common mistake when learning node.js (and common question here on stackoverflow). asynchronous operation completes indeterminate time in future , meanwhile, rest of code continues run. in fact resetpassword() function returns before stream has finished , before resetpassword result available.

thus cannot return result resetpassword function directly because function returns before result ready. instead, have pass in callback , result when callback called , caller of function have use callback rather directly returned result. can implement this:

// file * ./workflow/ssh.js  var client = require('ssh2').client; var conn = new client();  // var opts = require('optimist') //     .options({ //         user: { //             demand: true, //             alias: 'u' //         }, //     }).boolean('allow_discovery').argv;  // definition of reset password;  var resetpassword = function(user, host, loginuser, loginpassword, callback){  var command = 'echo -e "linuxpassword\nlinuxpassword" | passwd '+ user;  conn.on('ready', function() {   console.log('client :: ready');   conn.exec(command, function(err, stream) {     if (err) {         callback(err);         return;     }     stream.on('close', function(code, signal) {       console.log('stream :: close :: code: ' + code + ', signal: ' + signal);       conn.end();       callback(null, code);     }).on('data', function(data) {       console.log('stdout: ' + data);     }).stderr.on('data', function(data) {       console.log('stdlog: ' + data);     });   }); }).connect({   host: host,   port: 22,   username: loginuser,   password: loginpassword }); };  exports.resetpassword = resetpassword; 

and, can use this:

var ssh = require('./workflow/ssh.js');  ssh.resetpassword('test122', '192.168.0.101', 'root' , 'password', function(err, code) {     if (!err) {         console.log(code);     } }); 

p.s. original code, doesn't throw within async callback. exception goes whatever node.js infrastructure triggered async callback, not original code. that's why code has been changed communicate error via callback.

it node.js convention callbacks contain @ least 2 arguments. first argument error code should null if no error , appropriate error code or error object if there error. second argument (and other arguments want have) communicate successful result. useful follow convention since both consistent how node.js elsewhere , compatible mechanical error handling such done when interfaces modified use promises.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -