所以我使用的是亚马逊的产品广告API。我让它搜索并呈现大多数搜索的结果,但是如果API调用结果在某些字段中为零,它似乎会崩溃。
我试图通过使用三元操作符来克服它,但是这似乎仍然不起作用,缺少数据组件的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发布于 2016-03-28 18:31:36
你应该用散列键。现在?在三值操作中进行测试。
所以您的代码,而不是下面的代码:
@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、image、ASIN、URL、Feature或Price之一存在的情况下,才试图将该Title添加到@products数组中?如果是的话,那么您的代码就无法工作。您应该将其更新为以下内容:
if product.Title.present? || product.image.present? || product.ASIN.present? || product.URL.present? || product.Feature.present? || product.Price.present?
@products << product
endUPDATE:由于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
endhttps://stackoverflow.com/questions/36267800
复制相似问题