0

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

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

Why?

2 Answers2

1

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

{ 'socket.id' : data }
MrP
  • 1,291
  • 1
  • 9
  • 18
  • socket.id is a variable, if I do `` it won't be variable but a string. –  Jan 22 '14 at 14:26
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);
E_net4
  • 27,810
  • 13
  • 101
  • 139
  • This is what I did in the end. But it seemed like unnecessary writing for a simple task. –  Jan 22 '14 at 14:28
  • 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. – E_net4 Jan 22 '14 at 14:32