簡體   English   中英

如何使用node-config從數組中獲取項目屬性

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

我無法從配置文件中的數組中獲取項目。

{ 
 "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`)

不工作

希望有人會幫助,在此先感謝。

我不建議在配置中使用數組,因為從覆蓋角度將它們視為一個整體(例如字符串是字符數組,您不能覆蓋子字符串)。

就是說,有時候您想要這種行為。 當您這樣做時,以下應該工作

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

數字零(0)是變量名,類似於路徑中的其他名稱。 不知道為什么原來的問題在字符串中有一個“:”,但這是一個錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM