How can I get all object at a certain location?

The number of items I have is under 200.
But, if the code like below is executed,
variable: items would be huge number(≒endless loop),
and ,items are overlapped many times.
That array involve multiple :version of catalog_objects.
How can I get uniq and all item objects?
Please,help me.

(It suppose that the variables:client. location_id have been declered)

items=[]
cursor=nil
loop do
  result = client.catalog.search_catalog_items(
     body: {
        enabled_location_ids: [location_id],
        cursor: cursor,
        sort_order: "ASC",
        product_types: ["REGULAR"]
     }
  )
  if result.success?
     items.concat(result.body.items)
     break if result.cursor.nil?
     cursor = result.cursor
  elsif result.error?
     raise result.errors
  end
end

Hi @yasunao_izawa welcome to the forums!

In the Ruby SDK, cursor will be returned as an empty string, instead of nil. From the docs:

  • Make sure you get all items returned in a list call by checking the cursor value returned in the API response. When you call a list API the first time, set the cursor to an empty String or omit it from the API request. If the API response contains a cursor with a value, you call the API again to get the next page of items and continue to call that API again until the cursor is an empty String.

So, you can change break if result.cursor.nil? to break if result.cursor.empty? and that should end the loop correctly.

1 Like

Thank you for replying.
I tryed this, then I can solved problem!

1 Like