简体   繁体   English

我如何在nodejs中使用字符串作为变量

[英]How can i use a string as a variable in nodejs

I have some code in my app in nodeJs which generates a filter list based on data in the back-end.我在 nodeJs 的应用程序中有一些代码,它根据后端的数据生成一个过滤器列表。 There is static and dynamic filters.有 static 和动态滤波器。 For the dynamic filters i store the name of the query string in my config as a string.对于动态过滤器,我将查询字符串的名称作为字符串存储在我的配置中。 So the question is how can i use that string not as a string but as the name of an const which holds that query string.所以问题是我如何才能将该字符串不用作字符串,而是用作保存该查询字符串的 const 的名称。 Below is what my code looks like.下面是我的代码的样子。 data[i].queryname holds in this case the string tractList data[i].queryname在这种情况下包含字符串tractList

let result = await couch.n1qlQuery_wId('contacts', data[i].queryname ,[])
        for (let b = 0; b < result[0].length; b++) {
            let newOption = new option
            newOption.value = result[0][b].value_key
            newOption.name = result[0][b].name
            newOption.selected = false
            newFilter.options.push(newOption)

        }

here is what my const looks like这是我的 const 的样子

const tractList = 'select meta().id value_key, name from Contacts where _type = "tract_info" order by name'

A caveat;一个警告; I think the other answers are correct, and the correct way to do this is to have your config be an object, where you can look up values in it.我认为其他答案是正确的,正确的方法是让您的配置为 object,您可以在其中查找值。

This implies your config code, instead of looking like:这意味着您的配置代码,而不是看起来像:

const tractList = 'select meta().id value_key, name from Contacts where _type = "tract_info" order by name';
...

Should look more like:应该看起来更像:

const config: {
  tractList: 'select meta().id value_key, name from Contacts where _type = "tract_info" order by name',
  ...
}

This means you look this up fairly safely like this:这意味着您可以像这样相当安全地查找它:

let query = config[data[i].queryname];

The normal way would be to export this congi object from a module, so that you can import it anywhere.正常的方法是从模块中导出这个 congi object,这样你就可以在任何地方导入它。

So, to answer your actual question, if you are 100% sure that the variable you are looking for is in scope, and 1000% sure that the lookup variable is correct you can just eval the lookup variable.因此,要回答您的实际问题,如果您 100% 确定要查找的变量在 scope 中,并且 1000% 确定查找变量正确,您可以评估查找变量。

var query = eval(data[i].queryname);

The reason this is unsafe, is that eval will just compile whatever is in that function and run it, As you can imagine, if someone were to accidentally alter their querystring so that instead of 'tractList' it contained something like require("child_process").exec("rm -rf /") you would not be amused if you happened to compile and run it!这是不安全的原因,是 eval 只会编译 function 中的任何内容并运行它,正如您可以想象的那样,如果有人不小心更改了他们的查询字符串,以便它包含类似require("child_process").exec("rm -rf /")的东西而不是'tractList' require("child_process").exec("rm -rf /")如果你碰巧编译并运行它,你不会觉得好笑!

Also, as you can imagine, compiling new code every time you want to lok up a variable is going to be slow.此外,您可以想象,每次要查找变量时都编译新代码会很慢。

So, although you techincally can do this, I urge you not to, and instead to take the safer and faster route of defining your config as an object.因此,尽管您在技术上可以做到这一点,但我敦促您不要这样做,而是采用更安全、更快的方式将您的配置定义为 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM