I know that this error exists here, and I try to read and that code but I always get this Error: Callback was already called.
My code is:
switch(data.type) {
case 1:
console.log('blablabla');
break;
case 2:
/* SSH - login */
pool.query('SELECT INET_NTOA(ip) AS ip, pass, server FROM servers WHERE id=?',[data.id], function(err, rows, fields){
/* SSH - connection */
conn.connect({
host: rows[0].ip,
port: '22',
username: 'root',
password: decrypt(rows[0].pass, password)
})
/* SSH - ready */
conn.on('error', function(err) {
/* SSH - connection failed */
return callback('Login To <b>'+rows[0].server+'</b> Failed!');
})
conn.on('end', function() {
/* SSH - closed */
console.log('ssh closed...')
})
conn.on('ready', function() {
console.log('ready to execute terminal commands...')
})
})
break;
})
First time I call this function it works and I got
Login To Server Failed!
I call this function again and I got this error:
Error: Callback was already called.
After doing console.log I see that second time I call this function it is called twice so this is why this error about callback gets...so the problem is I try to put callback outside conn.on functions and inside but the second time that is called this function I get always this error..where in what place callbacks needs to be placed so that function does not get called twice?
UPDATE QUESTION:
I see when i add console.log to error and close event that each time i call this function i get one more event error and close called...see this:
First time called this function i get:
calling ssh...
error connecting to server...
ssh closed...
Second calling function:
calling ssh...
error connecting to server...
error connecting to server...
ssh closed...
ssh closed...
Third time calling function:
calling ssh...
error connecting to server...
error connecting to server...
error connecting to server...
ssh closed...
ssh closed...
ssh closed...
So is there a bug in SSH2 node js library or i im doing something wrong?
The events listenners are organised as an array; an regarding your code, the object << conn >> is external to the method you used. When you call it twice, you add a new listener to the array of the listenners of the object conn, making two listenners <<.on('error',... >> instead of one. See the picture below ( https://nodejs.org/api/events.html )
So you just need to initialize the object << conn >> before making a connection with it, to ensure that only one listener << on('error',... >> are affected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.