Troubleshooting GROQ query for accessing objects in an array from a Sanity schema
23 replies
Last updated: Mar 15, 2022
S
I am having a hard time reaching some objects in an array produced by a groq query. Maybe I need to write the query different, or find the object differently???
First, I have a schema for products. It looks something like this (simplified, of course):
Then, I have the groq query:
Which then creates this:
What I would like to do is grab the short description if it exists. If it doesn't, then grab the shopify description.
First, I have a schema for products. It looks something like this (simplified, of course):
name: "shopifyProduct", title: "Product", type: "document", fields: [ { name: "title", title: "Title", type: "string", }, { name: "descriptions", title: "Descriptions", type: "array", of: [ { type: "descriptionShopify" }, { type: "descriptionShortProduct" }, { type: "descriptionSEO" }, ], }, { name: "images", title: "Images", description: "the first image will be displayed as the main product image in this site", type: "array", of: [{ type: "reference", to: [{ type: "shopifyImage" }] }], }, ]}
const queryString = `*[_id in ['${referenceArray.join("','")}']]{ ..., descriptions[]{ _type == 'descriptionShortProduct' => { "shortDesc": description}, _type == 'descriptionShopify' => { "shopifyDesc": description } }, "productImage": images[0]-> {...} }`;
descriptions: 0: shopifyDesc: 0: {_key: '7559c0a5afdb', _type: 'block', children: Array(1), markDefs: Array(0), style: 'normal'} 1: {_key: '3723464efeb5', _type: 'block', children: Array(1), markDefs: Array(0), style: 'normal'} 2: {_key: 'bc1f9950ce6f', _type: 'block', children: Array(3), markDefs: Array(1), style: 'normal'} 1: shortDesc: 0: {_key: '342fd865500c', _type: 'block', children: Array(1), markDefs: Array(0), style: 'normal'} images: (5) [{ā¦}, {ā¦}, {ā¦}, {ā¦}, {ā¦}] productImage: {_createdAt: '2022-02-15T22:19:24Z', _id: 'loems-14084081090700', _rev: 'ysQBVdnoEMKFWYdngqdrGZ', _type: 'shopifyImage', _updatedAt: '2022-02-15T22:19:46Z', ā¦} title: "Miles and Jax"
Mar 11, 2022, 7:18 PM
S
I changed the query to be this:
But I am getting this error:
{description: 'Attribute or a string key expected', type: 'queryParseError'}
What am I doing wrong?
const queryString = `*[_id in ['${referenceArray.join("','")}']]{ ..., *[_type == 'descriptionShortProduct']{ "shortDesc": coalesce(description, 'unknown')}, *[_type == 'descriptionShopify']{ "shopifyDesc": coalesce(description, 'unknown')}, "productImage": images[0]-> {...} }`;
{description: 'Attribute or a string key expected', type: 'queryParseError'}
What am I doing wrong?
Mar 14, 2022, 4:27 PM
Ah, you need to add a key before your projections:
const queryString = `*[_id in ['${referenceArray.join("','")}']]{ ..., 'insert-key-here': *[_type == 'descriptionShortProduct']{ "shortDesc": coalesce(description, 'unknown')}, 'insert-other-key-here': *[_type == 'descriptionShopify']{ "shopifyDesc": coalesce(description, 'unknown')}, "productImage": images[0]-> {...} }`;
Mar 14, 2022, 4:32 PM
S
oh, ok, so the key was in the wrong spot
Mar 14, 2022, 4:33 PM
S
I will try that out
Mar 14, 2022, 4:33 PM
S
Sorry to keep bugging you. I swear I am trying to figure this out without pinging you, but I am just struggling š¬
So, this is a screenshot of a returned product. You can see the list of descriptions, as well as the empty "shortDesc" and "shopifyDesc" towards the bottom. I am unable to get any data to show up for those new keys
So, this is a screenshot of a returned product. You can see the list of descriptions, as well as the empty "shortDesc" and "shopifyDesc" towards the bottom. I am unable to get any data to show up for those new keys
Mar 14, 2022, 5:12 PM
S
Sorry to keep bugging you. I swear I am trying to figure this out without pinging you, but I am just struggling š¬
So, this is a screenshot of a returned product. You can see the list of descriptions, as well as the empty "shortDesc" and "shopifyDesc" towards the bottom. I am unable to get any data to show up for those new keys
So, this is a screenshot of a returned product. You can see the list of descriptions, as well as the empty "shortDesc" and "shopifyDesc" towards the bottom. I am unable to get any data to show up for those new keys
Mar 14, 2022, 5:12 PM
S
Sorry to keep bugging you. I swear I am trying to figure this out without pinging you, but I am just struggling š¬
So, this is a screenshot of a returned product. You can see the list of descriptions, as well as the empty "shortDesc" and "shopifyDesc" towards the bottom. I am unable to get any data to show up for those new keys
So, this is a screenshot of a returned product. You can see the list of descriptions, as well as the empty "shortDesc" and "shopifyDesc" towards the bottom. I am unable to get any data to show up for those new keys
Mar 14, 2022, 5:12 PM
Ah, you likely just don't need the projections if it's property inside of the object. Are 'descriptionShortProduct' and 'descriptionShopify' fields on the document?
Mar 14, 2022, 6:25 PM
Ah, you likely just don't need the projections if it's property inside of the object. Are 'descriptionShortProduct' and 'descriptionShopify' fields on the document?
Mar 14, 2022, 6:25 PM
S
The descriptionShortProduct and descriptionShopify are in the schema for the product - is that what you are asking?
{ name: "descriptions", title: "Descriptions", type: "array", of: [ { type: "descriptionShopify" }, { type: "descriptionShortProduct" }, { type: "descriptionSEO" }, ], },
Mar 14, 2022, 6:26 PM
I think I need to play around with your actual data to figure this out š . Can you share the URL that Vision give you for one of your queries?
Mar 14, 2022, 6:30 PM
S
I am putting the query in the code since I am referencing variables, so I am not sure if this is helpful or not: https://t5kmb8b3.api.sanity.io/v2021-10-21/data/query/dev?query=*%5B_type%20%3D%3D%20'shopifyProduct'%5D
Mar 14, 2022, 6:33 PM
S
Thank you! š
Mar 14, 2022, 6:35 PM
S
Any luck?
Mar 15, 2022, 2:28 PM
S
Any luck?
Mar 15, 2022, 2:28 PM
Not yet! It's more complicated than I initially thought. Still playing around with it, though.
Mar 15, 2022, 8:15 PM
Give this a try:
'description':coalesce(descriptions[@._type == 'descriptionShortProduct'], descriptions[@._type == 'descriptionShopify'])
Mar 15, 2022, 8:20 PM
S
it's working
Mar 15, 2022, 10:37 PM
S
I split it into two
then did the following in my widget to see if would finally get the right info:
'shortDescription':coalesce(descriptions[@._type == 'descriptionShortProduct']), 'shopifyDescription':coalesce(descriptions[@._type == 'descriptionShopify']),
if (shortDescription.length > 0){ description = "short" } else if (shopifyDescription.length > 0) { description = "shopify" } else { description = "empty" }
Mar 15, 2022, 10:38 PM
S
Thank you for your help! There is a lot I have to learn about groq
Mar 15, 2022, 10:39 PM
There's so much going on with GROQ. It takes a while to really get a feel for the complex stuff.
Mar 15, 2022, 10:40 PM
Sanityā build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.