I can not execute a query within a socket event using node.js (socket.io), why?

0

Here's what I did initially:

var 
http         = require('http'),
express      = require('express'),
mysql        = require('promise-mysql'),
mysql2       = require('mysql'),
parser       = require('body-parser'),
path         = require("path"),
fs           = require('fs'),
app          = express(),
serverhttp   = http.createServer(app),
io           = require('socket.io')(serverhttp),
pool         = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'chat_corporativo',
    connectionLimit: 100
});    

...

io.on('connection', function(socket){
    clients.push(socket);
    console.log('Conexão: %s sockets conectados! ID: %s', clients.length, socket.id);

    socket.on('ao_conectar_usuario', (data) => {
        pool.query('select * from usuarios where USUCOD = ?', [data]).
            then((rows) => {
                if (rows.length != 0){
                        let Usuariologado = rows[0].USUON;
                        if  (Usuariologado == 'N') {

                            let usuario = 
                            {
                                conexaonova : {
                                        idsocket: socket.id,
                                        codigousuario: data
                                    }
                            };

                            pool.query("update usuarios set USUON = 'S', USUSOCKET = ? where USUCOD = ?", [socket.id, data]).
                            then((rows) => {                                    
                                io.emit(usuario);
                            }).catch((err) => {
                                console.log(err);
                            });
                        } else {

                            let negarconexao = 
                            {
                                conexaonova : {                                        
                                    idsocket: '',
                                    porque: 'logado'}
                            };

                            socket.emit(negarconexao);
                        };
                    }                   
            }).catch((err) => {
                console.log(err);
            });
    }); 

    socket.on('mensagem_privada', (id, msg) => {            
        console.log(id + ' - ' + msg);
        var usuario_enviou;
        var usuario_receber;    

        pool.query("select * from usuarios where USUSOCKET = '?'", [id])
        .then((rows) =>{
            usuario_enviou = rows[0].CONCOD;
        }).catch((err) => {
            console.log(err);
        });  

        pool.query('select * from usuarios where USUSOCKET = ?', [socket.id])
        .then((rows) =>{
            usuario_receber = rows[0].CONCOD;
        }).catch((err) => {
            console.log(err);
        });               
    });

This block does not run, do not misunderstand, the event is captured, but what it does inside it, can not be executed.

socket.on('mensagem_privada', (id, msg) => {            
    console.log(id + ' - ' + msg);
    var usuario_enviou;
    var usuario_receber;    

    pool.query("select * from usuarios where USUSOCKET = '?'", [id])
    .then((rows) =>{
        usuario_enviou = rows[0].CONCOD;
    }).catch((err) => {
        console.log(err);
    });  

    pool.query('select * from usuarios where USUSOCKET = ?', [socket.id])
    .then((rows) =>{
        usuario_receber = rows[0].CONCOD;
    }).catch((err) => {
        console.log(err);
    });               
});

This second part, it can not send the information needed to insert into the stack of selections, even if I send the correct information, they are not running, I would like a light.

What do you suggest?

    
asked by anonymous 04.02.2018 / 19:17

0 answers