io.sockets.emit() unable to send info From server side To client side

主宰稳场 提交于 2020-03-23 12:01:19

问题


I am fetching the chat history from db and displaying it to the user. The data is successfully reading data from the mongodb but whenever I'm using io.sockets.emit then client side is unable to receive any data..

Below is the server.js code

io.sockets.on('connection',function(socket){
    socket.on('getChatHistory',function(data,callback){
        var query = { liveId: "1234" };
        db.collection("chat").find(query).toArray(function(err, result) { 
            if (err) throw err;
            console.log(result, " Result of Find query ...");
            io.sockets.emit('showChatHistory',  result);          
        });      
    });
});

Console is displaying the correct result and I am hoping to fetch the data on client side...

I'm capturing the data on client side in the following format:

{
socket.on('showChatHistory',function(data){
    console.log(data, "  data.......");
});

The following image is the output displayed after fetching record

and when i followed the error. It led to the following file

来源:https://stackoverflow.com/questions/60540668/io-sockets-emit-unable-to-send-info-from-server-side-to-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!