Ad

Associative Array Show Empty Value

- 1 answer

hi everyone I tried to develop chat app using node js and socket.io for back end , my problem is empty value of associated array

            socket.on('nickName', (data) => {                                           


    console.log(data)


    var roomId;

    con.query("SELECT user_id FROM nickname WHERE user_id = '" + String(data) + "'", (err, result) => {

        if (err) {
            throw err;
        }
        console.log(result)
        if (result == undefined || result[0] == undefined) {

            con.query("INSERT INTO nickname (user_id,socket_id) VALUES ('" + data + "','" + socket.id + "')")
        } else {
            con.query("UPDATE nickname  SET socket_id = '" + socket.id + "' WHERE user_id = '" + data + "' ", (err, result) => {

            })
        }
    })
    con.query("SELECT * FROM room WHERE user_id = '" + data + "'", (err, result) => {



        if(err){
            throw err;
        }else{
            io.to(socket.id).emit('getChatList', { "chatList": result })

            var unread = {}           
                        result.forEach(Element=>{

                           roomId = Element.room_name

                           con.query("SELECT COUNT(*) FROM message  WHERE room_id = '"+roomId+"' AND seen=0 AND to_user='"+data+"'",(err,unseen)=>{
                               if(err){
                                   throw err;
                               }else{

            // I trying set value to unread array
                                unread[roomId] =unseen[0]['COUNT(*)'] ;

                            // unread array work correctly   
                               console.log(unread)                                   
                               }              
                            })
                            // problem is here print unread empty ({})in 
                           // console.   
                            console.log(unread)  

                        })

        }

    })
});

unread array show empty value but when I put for example unread['A'] ='C' ; out of query block everything works fine , anyone can help me?

Ad

Answer

I solved my problem by using async.each() instead of result.forEach()

  socket.on('nickName', (data) => {
    console.log(data)

    var roomId;

    clients[data] = socket.id;
    con.query("SELECT user_id FROM nickname WHERE user_id = '" + String(data) + "'", (err, result) => {

        if (err) {
            throw err;
        }
        console.log(result)
        if (result == undefined || result[0] == undefined) {

            con.query("INSERT INTO nickname (user_id,socket_id) VALUES ('" + data + "','" + socket.id + "')")
        } else {
            con.query("UPDATE nickname  SET socket_id = '" + socket.id + "' WHERE user_id = '" + data + "' ", (err, result) => {

            })
        }
    })

    con.query("SELECT * FROM room WHERE user_id = '" + data + "'", (err, result) => {



        if(err){
            throw err;
        }else{
            io.to(socket.id).emit('getChatList', { "chatList": result })



            var unread = {};


                       asyncc.each(result,function(Element,callbackk){

                           roomId = Element.room_name
                           console.log(Element)

                           con.query("SELECT COUNT(*) FROM message  WHERE room_id = '"+Element.room_name+"' AND seen=0 AND to_user='"+data+"'",(err,unseen)=>{
                               if(err){
                                   throw err;

                               }else{

                           console.log(unseen)
                               console.log(Element.room_name)
                              unread[Element.room_name] = unseen[0]['COUNT(*)'] ;



                               }

                               callbackk();




                            })




                        },function(er){


                            console.log(unread)


                        })





        }


    })




});
Ad
source: stackoverflow.com
Ad