1

In the process of making a discord bot using node.js and discord.js, I've come to a point where I want to create a new variable, in this case an object with one property (the string "server" and some string for a value.) To name said new variable, I want to use the server ID of the server I'm referring to here, which is stored in another variable. What is a way I can do this?

So far I've tried eval("var " + serverID + " = {'server': 'test'}"), which gave me a syntax error: invalid/unexpected token on the second plus sign (replacing the object with a string still gave me the same error). Everywhere I've looked hasn't been helpful in explaining what is wrong with the eval function, and I'm confused as to how I would do this another way.

In case the first thing that came to your mind was restructuring how I'm working with variables and the types I'm using, whatever this outputs must let me add more information to this variable, which at least in my mind restricts me to using Objects and adding properties. I also store this variable to a JSON file later in the code which also restricts me to using either Arrays or Objects.

Colin Hanes
  • 106
  • 2
  • 12
  • Your example code runs fine for me, are you sure that's exactly how it looks in your code? Is the value of `serverID` a valid variable name? But honestly, I can't think of a practical use case for this that wouldn't be better served by storing it in an array or as an object property instead. As a general rule, if you're considering using `eval` 9 times out of 10 there's a better way to do what you're trying to do. – John Montgomery Apr 02 '20 at 22:07
  • @JohnMontgomery When I log ```serverID``` in the console right before the ```eval``` it outputs just fine, so there isn't anything wrong with the variable. The ```eval``` function is exactly as in my code, except for changing variable names to make them more readable here. – Colin Hanes Apr 02 '20 at 22:13
  • But what is its value? Just because you can log it doesn't mean it's a valid variable name, you can log anything. – John Montgomery Apr 02 '20 at 22:17
  • @JohnMontgomery the actual server ID is something like 694717308618187441, but I added 'settings' right before this line of `eval` code for naming purposes. `console.log(serverID)` outputs 694717308618187441settings correctly, so I thought there's no reason why 694717308618187441settings wouldn't work as a variable name. – Colin Hanes Apr 02 '20 at 22:24
  • That's why it wasn't working then, [JavaScript variable names must begin with `$`, `_`, or a letter.](https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names) – John Montgomery Apr 02 '20 at 22:28
  • Oops, had no idea about that restriction. My lack of formal instruction might be showing here... – Colin Hanes Apr 02 '20 at 22:32
  • You shouldn’t do that. – Sumi Straessle Apr 02 '20 at 22:37

3 Answers3

2

I'm going to guess that the eval fails because your serverID is a number (or a string that represents a number), so the statement would look like var 123 = {'server': 'test'}, which would give a syntax error.

In any case, a simple alternative would be to create a property on an object instead of a variable. Something like:

var myVariables = {};
//...
myVariables[serverID] = {'server': 'test'};

You could even add it to the global object, if it makes sense for your situation and you really need a global variable.

Aioros
  • 4,373
  • 1
  • 18
  • 21
  • I think this should work. For some reason having the value of a property as the object I was trying to store never crossed my mind. Server IDs are numbers, but I did add letters to the end for naming purposes earlier in the code. – Colin Hanes Apr 02 '20 at 22:21
  • Got it. If you really wanted to keep your original approach, all you needed was to add letters (or `_` or `$`) at the beginning, because a variable identifier can't start with a number. – Aioros Apr 03 '20 at 12:57
  • I ended up making an object with names of the server IDs and values of the original object I wanted to store. For some reason I had forgotten you could store objects inside properties of other objects. – Colin Hanes Apr 03 '20 at 20:13
0

There is no good reason to to store a variable as the name of another variable. The simplest way to accomplish what you want, as you mentioned, is storing the serverID in the object.

Brendan C.
  • 171
  • 5
  • Hmmm, not exactly sure what you mean by 'storing the serverID in the object." In that case, what would be the name of said object and then what would happen to the name:value pair I have inside at the moment. – Colin Hanes Apr 02 '20 at 22:15
0

Your syntax is correct, although you may want to ensure that your server ID is a legal JavaScript identifier.

var serverID = "asddkjkisdf";
eval("var " + serverID + " = {'server': 'test'}")

console.log(asddkjkisdf);

I would however recommend creating a data structure like an array of objects to store this information. In that case you wouldn't have to worry about the value of your server ID. E.g.

[{id: 'asdfasdf', server: 'test'}, ... ]
Ed Lucas
  • 5,955
  • 4
  • 30
  • 42
  • This is the issue with my code. Because `serverID` started with a number, it wasn't a valid JavaScript variable name. Didn't think to check that earlier... – Colin Hanes Apr 02 '20 at 22:30