Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Socket.io返回null作为数据,但服务器正在接收数据_Node.js_Mongodb_Express_Mongoose_Socket.io - Fatal编程技术网

Node.js Socket.io返回null作为数据,但服务器正在接收数据

Node.js Socket.io返回null作为数据,但服务器正在接收数据,node.js,mongodb,express,mongoose,socket.io,Node.js,Mongodb,Express,Mongoose,Socket.io,我不熟悉nodes、express、mongodb和socket.io。到目前为止,事情进展得相当顺利。因此,我正在构建一个小的测试应用程序,用户在其中输入自己的名字,然后发布一个问题 socket.io接收数据用户名和问题,但返回数据时表示为null。一开始它是有效的,但后来我开始安装猫鼬,从那时起事情就出了问题。我使用express framework,并安装了所有必要的模块 在我看来,这是连接到套接字和更新数据的代码: <script> $(document).read

我不熟悉nodes、express、mongodb和socket.io。到目前为止,事情进展得相当顺利。因此,我正在构建一个小的测试应用程序,用户在其中输入自己的名字,然后发布一个问题

socket.io接收数据用户名和问题,但返回数据时表示为null。一开始它是有效的,但后来我开始安装猫鼬,从那时起事情就出了问题。我使用express framework,并安装了所有必要的模块

在我看来,这是连接到套接字和更新数据的代码:

<script>
    $(document).ready(function()
    {
        var socket = io();

        $("#btnAskQuestion").on("click", function(e)
        {
            //get the value of the inputfield and textarea
            var username = $('#inputQuestion').val();
            var question = $('#question').val();
            var result = "<p>Username: </p>" + username + " <br /><br /> " + 
            "<p>Question: </p>" + question + " <br /> <br /> ";
            console.log("Values of inputfield & textarea: " + result);

            //makes the connection to the server and send the data 
            var socket = io.connect('http://localhost:3000');
            socket.emit('sendQuestion', result);
            console.log("Gets send: " +result);
            //alert('test');
        });

        //returns the updated data + visualize it on the page
        socket.on('updateQuestion', function (data) 
        {
            console.log("Updated data: " + data);//data is the username and question = result
            $('#answer').css("border", "4px solid #9C27B0");
            $('#answer').css("borderRadius", "8px");
            $('#answer').css("padding", "10px");
            $('#answer').css("fontSize", "16px");
            $('#answer').append('<li>'+data+'</li>');
        }); 
    });
</script>
在chrome浏览器中,我运行开发者工具,输出如下屏幕截图:

这是服务器端记录数据的屏幕截图

我希望有人能帮我,因为我不明白为什么它会返回空值


提前谢谢!!1

[编辑:刚刚注意到您实际上正在发送数据。结果一开始是空的]

而不是发送数据。结果发送数据


注意,这将给出整个字符串username+问题。如果您只想要这个问题,那么您必须在服务器端解析接收到的字符串,并且只向连接的客户端发出问题

什么是返回空值?代码中没有返回语句,所以我不理解您的意思。
var io = require('socket.io')(server);
io.on('connection', function (socket) 
{
   socket.on('sendQuestion', function (data) 
  {
     console.log("The server received a question");
     console.log(data);

    // send question to ALL clients
    io.sockets.emit("updateQuestion", data.result);
  });
});