I cannot run a query inside a socket event using Node.js (socket.io), why?

Asked

Viewed 62 times

0

Follow 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 doesn’t run, don’t get it wrong, the event is captured, but what it does inside it, it can’t run.

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 fails to send the necessary information to insert in the stack of selections, even if I send the correct information, they are not running, would like a light.

What do you suggest?

  • Have any return error? Debug to find out what’s going on.

  • The error is that there is no data reported in the variables usuario_receber and usuario_enviou, when I try to use them, that is, it is not forwarding the querys on the connection to use the Promise and return something for use.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.