【问题标题】:How can I get item property from an array with node-config如何使用 node-config 从数组中获取项目属性
【发布时间】:2019-05-13 15:31:20
【问题描述】:

我无法从配置文件中的数组中获取项目。

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

我正在尝试获取第一个搜索项的 SearchInitUrl 属性,并且我正在使用

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

没用

希望有人能提供帮助,在此先感谢。

【问题讨论】:

    标签: node-config


    【解决方案1】:

    我不推荐在配置中使用数组,因为从覆盖的角度来看它们被视为一个整体(就像字符串是字符数组,你不能覆盖子字符串)。

    也就是说,有时您想要这种行为。当你这样做时,以下应该工作

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

    数字零 (0) 是变量名称,与路径中的其他名称一样。不知道为什么原始问题在字符串中有一个“:”,但这是一个错误。

    【讨论】:

    • (Loren West 是 node-config 的作者)。
    • 在 wiki 的 Common Usage 页面上将是很好的文档数组支持。
    • 非常感谢。我使用了像点网配置这样的节点配置,我的错。谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2011-11-30
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    相关资源
    最近更新 更多