嗨,这是我的Node.JS应用程序的一部分,使用参数msg.to调用findSocketByUID,这是一个字符串值。 但为什么函数findSocketByUID中的参数uid未定义? 如何正确调用findSocketByUID传递参数?
编辑:我在这一行添加一个断点:
var socket_to = findSocketByUID(msg.to);我的msg看起来像这样:
var sockets = []; function findSocketByUID(uid){ var index = findIndexByUID(uid); if(index == -1) return null; return sockets[index]; } socket.on('chat message', function(msg){ if(msg.to == 'all'){ io.emit('chat message', msg); }else{ var socket_to = findSocketByUID(msg.to); if(socket_to){ socket_to.emit("chat message", msg); }else{ socket.broadcast.emit("chat message", msg); } } });“{”to“:”a27efd3f-ee63-413a-20ae-527419fab246“,”content“:”giuh“,”time“:”2016-06-06T11:27:11Z“,”from“:”683C2782-A575 -4154-9F78-CAD6B74AB19D“}”
Hi, this is part of my Node.JS application, findSocketByUID is called with an argument msg.to, which is a string value. But why parameter uid in function findSocketByUID is undefined? How do I call findSocketByUID correctly to pass an argument?
Edit: I add a breakpoint at this line:
var socket_to = findSocketByUID(msg.to);and my msg looks like this:
"{ "to" : "a27efd3f-ee63-413a-20ae-527419fab246", "content" : "giuh", "time" : "2016-06-06T11:27:11Z", "from" : "683C2782-A575-4154-9F78-CAD6B74AB19D" }"
最满意答案
编辑中显示的msg似乎是一个JSON字符串,而不是一个实际的对象(它在大括号外面有引号)。 字符串不具有.to属性。
您需要使用JSON.parse()将其转换为实际对象,以便您可以访问其属性。
The msg shown in your edit seems to be a string of JSON, not an actual object (it has quote marks outside the curly brackets). A string won't have a .to property.
You'll need to use JSON.parse() to convert it to an actual object so that you can access its properties.
更多推荐
发布评论