0

I have a function like this:

function dataSend(type, message) {
  return {
    type: message
  };
};

Which I am using like this:

io.sockets.emit('addToQueue', dataSend('error', 'User not found'));

But when it gets to the client side I get an object that is like

{ type: 'User not found' } instead of { 'error': 'User not found' }

Why is this acting like this? I'm not sure how to fix this any information would be great thanks.

Datsik
  • 14,453
  • 14
  • 80
  • 121

2 Answers2

3

You can't set the key with a variable like that, you'll need bracket notation

function dataSend(type, message) {

   var obj = {};

   obj[type] = message;

   return obj;

};
adeneo
  • 312,895
  • 29
  • 395
  • 388
1

Try this:

function dataSend(type, message) {
  var a={};
  a[type]=message;
  return a;
}

Javascript doesn't require quotes around the property names.

Using {type:message} or {'type':message} produces the same result.

Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42