0

I'm receiving this error when I try to start node:

console.dir({socket.id:data});
                   ^

Why?

1

2 Answers 2

1

When using JSON to describe an object, the key names must directly translate to a string literal (as in, not refer to other variable identifiers). If you want another object's value to be the key name of the variable, you may try this:

var o = {};
o[socket.id] = data;
console.dir(o);
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I did in the end. But it seemed like unnecessary writing for a simple task.
It just happens to be part of the JavaScript syntax. Whether declaring an object in JSON format ({something: 1}) or using dot notation (o.something = 1), key names are interpreted as strings directly, converted from other types if needed.
1

You cannot use a . in object key's names. If you really want to do this, use

{ 'socket.id' : data }

1 Comment

socket.id is a variable, if I do `` it won't be variable but a string.

Your Answer

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