2

I'm wrapping data to a Node.js WebSocket server in JSON. I'm trying to log the contents of the JSON object server side, but Regardless of the method I try, Node just logs object Object

I have tried

console.log(message.data)
console.log(util.inspect(message.data, {showHidden: true, depth: null}));
console.log(JSON.stringify(message.data));

Why would Node.js log object Object ? is the JSON malformed?

I'm sending VERY simple JSON objects from the client. Things like

ws.send({'a': 0, 'b': 1});
5
  • does it at least log null? if it doesn't log anything at all, chances are, it never got there. Commented Dec 20, 2016 at 19:37
  • As stated in the question, it just logs "object Object" regardless of the log method. Now, if I try to log message.data.a it logs undefined Commented Dec 20, 2016 at 19:38
  • 1
    try to send it to front end and log in the browser console. May help Commented Dec 20, 2016 at 19:38
  • 1
    so, it's logging the value exactly as it is. :p Commented Dec 20, 2016 at 19:41
  • Yes, that's exactly what was happening. the client was converting the object literal. Look at @Dr Goat 's answer below.. Commented Dec 20, 2016 at 19:42

1 Answer 1

3

The client might be sending object Object, over the websocket, not the server being unable to parse the object. Try to stringify the object before you send it.

ws.send(JSON.stringify({'a': 0, 'b': 1})); //client

console.log(JSON.parse(message.data)); //server 
Sign up to request clarification or add additional context in comments.

8 Comments

GOOD CATCH! That's exactly what it was. I'll accept asap.
@NativeCoder Thanks, may I suggest using the rubber duck method, as it helps catch errors like this.
@NativeCoder Not exactly, heres a link to it. blog.codinghorror.com/rubber-duck-problem-solving
@NativeCoder You got him wrong. Just google rubber duck debugging, read the wiki and you'll get it.
@NativeCoder Now you get it :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.