Return Images with product query

swift
images
products

#1

I’m querying products from my app to match a search parameter. The search works fine but I can’t get the image of that product. It gives me an image ID in the “relationships” returns, giving me a “main_image” { … }, but in my JSON response that contains the price and so on, my main_image is nil.

How do I pass in the image ID that I’m receiving to then retrieve the actual image on my viewController?

Here is my query at the moment:

let newQuery = MoltinQuery(
            offset: nil,
            limit: nil,
            sort: nil,
            filter: "eq(description, \(String(describing: searchText)))",
            include: [])

I tried to include "main_image: in the include: section but no avail.

Thanks for the help!


#2

Hi Eoghan,

You will need the main_image in the includes to make this work. Your query should look like this:

let newQuery = MoltinQuery(
            offset: nil,
            limit: nil,
            sort: nil,
            filter: "eq(description, \(String(describing: searchText)))",
            include: [.main_image])

That should fulfil the main_image property on a Product object. You can see the commit that adding that in here

I did add this in before fixing up some issues with our CocoaPods set up, so your existing installation may not have it. You could either run pod install again, or update your Podfile to specify the installation as pod 'Moltin', '2.0.0'.

Let me know if this works for you.


Implementing a search function on all Products
#3

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.