关联数组显示空值

时间:2019-06-17 11:22:24

标签: node.js socket.io

大家好,我尝试使用node js和socket.io作为后端来开发聊天应用程序,我的问题是关联数组的值为空

            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['A'] ='C' ;放在查询块之外时,一切工作正常,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我通过使用async.each()而不是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)


                        })





        }


    })




});