Note that there are some explanatory texts on larger screens.

plurals
  1. POA single row in my .csv fails to import due to TypeError: can't convert String into Integer, though others pass
    primarykey
    data
    text
    <p>Similar to <a href="https://stackoverflow.com/questions/16293444/cant-change-strings-into-int-in">this question...</a> </p> <p>When importing a CSV, I get the following Ruby 1.9.3 error: "TypeError: can't convert String into Integer"</p> <p>It reads in full:</p> <pre><code>pry(main)&gt; Lightbulb.import TypeError: can't convert String into Integer from /Users/user/.rvm/gems/ruby-1.9.3-p448/bundler/gems/rails-1c2717d3f5a3/activesupport/lib/active_support/core_ext/object/try.rb:36:in `[]' </code></pre> <p>I believe this is because we're passing a string (Amazon ASIN) as an index to the array in lightbulb.rb, as well as importing other data from the Amazon Product Inventory.</p> <p>It works for all but on of my 35 rows. I think it is because the data for this particular Amazon product cannot be converted to an integer? I don't really know...</p> <p>This is the import process which fails with one row in the CSV (listed below) where the string cannot be converted to an integer:</p> <pre><code> # asin organization_name lumens_per_watt light_output watts life_hours light_color def self.import destroy_all table = CSV.read("#{Rails.root}/vendor/data/bulb.csv", :headers =&gt; true) table.each do |row| ap = Product.find_or_save(row['asin']) create( :price =&gt; ap.lowest_new_price, :small_image =&gt; ap.small_image, :medium_image =&gt; ap.medium_image, :large_image =&gt; ap.large_image, :detail_page_url =&gt; ap.detail_page_url, :asin =&gt; ap.asin, :brand =&gt; row['organization_name'], :feature =&gt; ap.feature, :organization_name =&gt; row['organization_name'], :label =&gt; ap.label, :manufacturer =&gt; row['organization_name'], :product_model =&gt; ap.product_model, :sku =&gt; ap.sku, :title =&gt; ap.title, :total_new =&gt; ap.total_new, :editorial_reviews =&gt; ap.editorial_reviews, :efficiency =&gt; row['lumens_per_watt'], :brightness =&gt; row['light_output'], :actual_watts =&gt; row['watts'], :expected_life =&gt; row['life_hours'], :light_color =&gt; row['light_color'], :perceived_watts =&gt; calculate_perceived_watts(row['light_output']) ) end end </code></pre> <p>Here is the full code for Product.find_or_save:</p> <pre><code>class Product include Mongoid::Document include Mongoid::Timestamps field :asin, type: String field :parent_asin, type: String field :detail_page_url, type: String field :item_links, type: Array, default: [] field :sales_rank, type: String field :small_image, type: String field :medium_image, type: String field :large_image, type: String field :image_sets, type: String, default: [] field :brand, type: String field :ean, type: String field :ean_list, type: Hash, default: {} field :feature, type: Array, default: [] field :item_dimensions, type: Hash, default: {} field :label, type: String field :manufacturer, type: String field :product_model, type: String field :mpn, type: String field :package_dimensions, type: Hash, default: {} field :part_number, type: String field :product_group, type: String field :product_type_name, type: String field :publisher, type: String field :sku, type: String field :studio, type: String field :title, type: String field :lowest_new_price, type: String field :lowest_used_price, type: String field :total_new, type: String field :total_used, type: String field :offers, type: Hash, default: {} field :customer_reviews, type: String field :editorial_reviews, type: String field :similar_products, type: Array, default: [] field :nodes, type: Array, default: [] def self.find_or_save(asin) if item = where(asin: asin).first item else amazon_product_hash = AmazonApi.product(asin) attrs = ProductWrapper.parse(amazon_product_hash) create(attrs) end end </code></pre> <p>If I have one row that fails - just one! :)</p> <pre><code>B00B4CPKT4,Philips,56,730,13,25000,2700 </code></pre> <p>If I remove that row, just 1 of 35, it runs perfectly. It always fails at that one row in the .csv. I have placed it first, in the middle, and last - even rewritten it to find any hidden gremlin characters.</p> <p>Here is the header and first row, for example, which work flawlessly:</p> <pre><code>asin,organization_name,lumens_per_watt,light_output,watts,life_hours,light_color B00BXG7UZ8,Cree,75,450,6,25000,2700 </code></pre> <p>I tried adding <code>.to_i</code> to make <code>:asin =&gt; ap.asin.to_i,</code> but no luck!</p> <p>This is the last line I see in development.log, and I see 13 instances of this line corresponding to the 13 frustrating times I tried to run Lightbulb.import with this faulty product in the bulb.csv file:</p> <pre><code> MOPED: 54.234.253.6:33037 QUERY database=heroku_app14604604 collection=products selector={"$query"=&gt;{"asin"=&gt;"B00B4CPKT4"}, "$orderby"=&gt;{:_id=&gt;1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (5231.9772ms) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload