API调用=未定义的方法`[]'为零:NilClass

时间:2016-03-28 17:47:13

标签: ruby-on-rails amazon-product-api

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

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

undefined method `[]' for nil:NilClass

以下是我的控制器的相关代码:

原创尝试:

@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

使用中的三元运算符:

@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

1 个答案:

答案 0 :(得分:0)

你应该使用hash ['key']。present?在三元运行中进行测试。

所以你的代码,而不是以下代码:

@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

应如下:

@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

但我不太明白你想在这里实现的目标:

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

您是否尝试将product添加到@products数组,仅当TitleimageASINURL之一时,FeaturePrice存在?如果是,那么您的代码不起作用。您应该将其更新为以下内容:

if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present?
  @products << product
end

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

@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