1

I am coding in node.js atm. I need to create a dynamic variable and invoke it.

e.g.:

username = 'im_a_user';

global['ws[' + username + ']'] = ws; //(yes, i want to store the connection with ws module)

but

ws[im_a_user].send('blabla');

doesn't work and node shuts down. So I want to know how global['ws[' + username + ']'] looks like for debbuging.


Do you know how I can print it - or even better, why im_a_user in ws[im_a_user].send('blabla'); isn't defined?

Thanks for your time!

1 Answers1

0

Accessing object property is possible with brackets, but you have to give it a valid expression: a string literal or a variable. Looks like you are referencing a variable that is not defined. That's where Node chokes.

So try either with a variable that is defined

var key = 'im_a_user'
ws[key].send('blabla');

or a string literal

ws['im_a_user'].send('blabla');
pspi
  • 11,189
  • 1
  • 20
  • 18