首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于nil:NilClass的API调用=未定义方法‘[]’

用于nil:NilClass的API调用=未定义方法‘[]’
EN

Stack Overflow用户
提问于 2016-03-28 17:47:13
回答 1查看 134关注 0票数 0

所以我使用的是亚马逊的产品广告API。我让它搜索并呈现大多数搜索的结果,但是如果API调用结果在某些字段中为零,它似乎会崩溃。

我试图通过使用三元操作符来克服它,但是这似乎仍然不起作用,缺少数据组件的API调用会导致以下错误消息:

代码语言:javascript
复制
undefined method `[]' for nil:NilClass

下面是来自我的控制器的相关代码:

最初的尝试:

代码语言:javascript
复制
@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
    product = OpenStruct.new
    product.Title = item['ItemAttributes']['Title']
    product.image = item['MediumImage']['URL']
    product.ASIN = item['ASIN']
    product.URL = item['DetailPageURL']
    product.Feature = item['ItemAttributes']['Feature']
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice']
    @products << product
end

使用中的三元操作符:

代码语言:javascript
复制
@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
    product = OpenStruct.new
    product.Title = item['ItemAttributes']['Title'] ? item['ItemAttributes']['Title'] : nil
    product.image = item['MediumImage']['URL'] ? item['MediumImage']['URL'] : nil
    product.ASIN = item['ASIN'] ? item['ASIN'] : nil
    product.URL = item['DetailPageURL'] ? item['DetailPageURL'] : nil
    product.Feature = item['ItemAttributes']['Feature'] ? item['ItemAttributes']['Feature'] : nil
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil
      @products << product
    end
end
EN

回答 1

Stack Overflow用户

发布于 2016-03-28 18:31:36

你应该用散列键。现在?在三值操作中进行测试。

所以您的代码,而不是下面的代码:

代码语言:javascript
复制
@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
    product = OpenStruct.new
    product.Title = item['ItemAttributes']['Title'] ? item['ItemAttributes']['Title'] : nil
    product.image = item['MediumImage']['URL'] ? item['MediumImage']['URL'] : nil
    product.ASIN = item['ASIN'] ? item['ASIN'] : nil
    product.URL = item['DetailPageURL'] ? item['DetailPageURL'] : nil
    product.Feature = item['ItemAttributes']['Feature'] ? item['ItemAttributes']['Feature'] : nil
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'] ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil
      @products << product
    end
end

应如下:

代码语言:javascript
复制
@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
    product = OpenStruct.new
    product.Title = item['ItemAttributes']['Title'].present? ? item['ItemAttributes']['Title'] : nil
    product.image = item['MediumImage']['URL'].present? ? item['MediumImage']['URL'] : nil
    product.ASIN = item['ASIN'].present? ? item['ASIN'] : nil
    product.URL = item['DetailPageURL'].present? ? item['DetailPageURL'] : nil
    product.Feature = item['ItemAttributes']['Feature'].present? ? item['ItemAttributes']['Feature'] : nil
    product.Price = item['OfferSummary']['LowestNewPrice']['FormattedPrice'].present? ? item['OfferSummary']['LowestNewPrice']['FormattedPrice'] : nil

    if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil
      @products << product
    end
end

但我不太明白你在努力实现什么:

代码语言:javascript
复制
if product.Title || product.image || product.ASIN || product.URL || product.Feature || product.Price === nil
  @products << product
end

是否只在productimageASINURLFeaturePrice之一存在的情况下,才试图将该Title添加到@products数组中?如果是的话,那么您的代码就无法工作。您应该将其更新为以下内容:

代码语言:javascript
复制
if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present?
  @products << product
end

UPDATE:由于item的任何或所有属性都是可选的,您可以使用以下方法,而不是三元操作:

代码语言:javascript
复制
@hashed_products['ItemSearchResponse']['Items']['Item'].each do |item|
  product = OpenStruct.new
  product.Title = item.try(:[], 'ItemAttributes').try(:[], 'Title')
  product.image = item.try(:[], 'MediumImage').try(:[], 'URL')
  product.ASIN = item.try(:[], 'ASIN')
  product.URL = item.try(:[], 'DetailPageURL')
  product.Feature = item.try(:[], 'ItemAttributes').try(:[], 'Feature')
  product.Price = item.try(:[], 'OfferSummary').try(:[], 'LowestNewPrice').try(:[], 'FormattedPrice')

  if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present?
    @products << product
  end
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36267800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档