简体   繁体   English

如何使用node-config从数组中获取项目属性

[英]How can I get item property from an array with node-config

I can not get item from an array in the configuration file. 我无法从配置文件中的数组中获取项目。

{ 
 "crawlerConfigurations":{
        "ClickDelay": 100,
        "TypeDelay": 200,
        "Selectors": {
            "Search": [
                {
                    "SearchInitUrl": "https://www.wukong.com"
                 }
          ]
  }
}

I'm trying to get the SearchInitUrl prop of first Search item, and I'm using 我正在尝试获取第一个搜索项的SearchInitUrl道具,并且正在使用

config.get(`crawlerConfigurations.Selectors.Search[0].SearchInitUrl`)
config.get(`crawlerConfigurations.Selectors.Search.[0].SearchInitUrl`)

did not work 不工作

Hope someone would help, thanks in advance. 希望有人会帮助,在此先感谢。

I don't recommend arrays in configs because they're treated as a whole from an override perspective (like strings are arrays of characters, and you can't override a substring). 我不建议在配置中使用数组,因为从覆盖角度将它们视为一个整体(例如字符串是字符数组,您不能覆盖子字符串)。

That said, sometimes you want that behavior. 就是说,有时候您想要这种行为。 When you do, the following should work 当您这样做时,以下应该工作

config.get('crawlerConfigurations.Selectors.Search.0.SearchInitUrl');

The number zero (0) is a variable name like the other names in the path. 数字零(0)是变量名,类似于路径中的其他名称。 Not sure why the original question had a ':' in the string, but that was a bug. 不知道为什么原来的问题在字符串中有一个“:”,但这是一个错误。

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

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