Querying for arrays in arrays in Sanity.io

33 replies
Last updated: May 21, 2021
Couple of questions if anyone has time to help!
Why would the first return results, but not the second? How can you match in from an array?

*[_type == "project" && "I Know the End" in tags]
*[_type == "project" && ["I Know the End", "themeTwo"] in tags]
May 21, 2021, 6:08 PM
Querying for arrays in arrays is not supported unfortunately. However, the funny thing is that it's actually possible 🙂
Credits to
user E
:
*[
  _type == "project" && 
  count(tags[@ in ["I Know the End", "themeTwo"]]) > 0
]
May 21, 2021, 6:13 PM
ah!
May 21, 2021, 6:14 PM
wonderful
May 21, 2021, 6:14 PM
Any idea why I might get this?

request.js?c471:27 
Uncaught (in promise) ClientError {response: {…}, statusCode: 400, responseBody: "{↵  "error": {↵    "description": "Invalid functio…n>, string)",↵    "type": "queryParseError"↵  }↵}", details: {…}, message: "Invalid function call: in(<unknown>, string)", …}
details: {description: "Invalid function call: in(<unknown>, string)", type: "queryParseError"}
response: {body: {…}, url: "<https://owp2y0vr.api.sanity.io/v1/data/query/produ…40%20in%20'I%20Know%20the%20End'%5D)%20%3E%200%5D>", method: "GET", headers: {…}, statusCode: 400, …}
responseBody: "{\n  \"error\": {\n    \"description\": \"Invalid function call: in(&lt;unknown&gt;, string)\",\n    \"type\": \"queryParseError\"\n  }\n}"
statusCode: 400
message: "Invalid function call: in(&lt;unknown&gt;, string)"
name: "ClientError"
stack: "ClientError: Invalid function call: in(&lt;unknown&gt;, string)\n    at onResponse (webpack-internal:///./node_modules/@sanity/client/lib/http/request.js:27:13)\n    at applyMiddleware (webpack-internal:///./node_modules/get-it/lib/util/middlewareReducer.js:14:23)\n    at onResponse (webpack-internal:///./node_modules/get-it/lib/index.js:83:22)\n    at eval (webpack-internal:///./node_modules/get-it/lib/index.js:49:16)\n    at onLoad (webpack-internal:///./node_modules/get-it/lib/request/browser-request.js:213:5)\n    at XMLHttpRequest.xhr.&lt;computed&gt; (webpack-internal:///./node_modules/get-it/lib/request/browser-request.js:77:5)"
_proto_: BaseError
May 21, 2021, 6:17 PM
My array looks like this but it doesn't like it:

(3) ["Coincidence", "themeTwo", "Art for Everyone", __ob__: Observer]
May 21, 2021, 6:20 PM
ah OK, thanks
user M
- wondering how I can get this working with a dynamic array
May 21, 2021, 6:26 PM
Could you filter beforehand to leave only the string type items?
May 21, 2021, 6:28 PM
yeh, I've just mapped it to a new array - but it's still not liking it
May 21, 2021, 6:29 PM
const query = groq`*[_type == "project" &amp;&amp; count(tags[@ in '${filters}']) &gt; 0]`;
May 21, 2021, 6:29 PM
thanks so much for your help
user M
May 21, 2021, 6:29 PM
feel like I'm close!
May 21, 2021, 6:29 PM
works perfectly when I copy and paste the exact console log
May 21, 2021, 6:31 PM
works perfectly when I copy and paste the exact console log
May 21, 2021, 6:31 PM
just get various errors "Undefined function \"the\"" etc
May 21, 2021, 6:34 PM
just get various errors "Undefined function \"the\"" etc
May 21, 2021, 6:34 PM
Have you tried without the single quotes?
const query = groq`*[_type == "project" &amp;&amp; count(tags[@ in ${filters}]) &gt; 0]`;
May 21, 2021, 6:35 PM
Have you tried without the single quotes?
const query = groq`*[_type == "project" &amp;&amp; count(tags[@ in ${filters}]) &gt; 0]`;
May 21, 2021, 6:35 PM
yeh, lots of combinations
May 21, 2021, 6:35 PM
that gives me the above error
May 21, 2021, 6:36 PM
What happens if you do something like this?
const query = `*[_type == "project" &amp;&amp; count(tags[@ in $filters]) &gt; 0]`
const params = { filters }

const data = client.fetch(query, params)
May 21, 2021, 6:37 PM
What happens if you do something like this?
const query = groq`*[_type == "project" &amp;&amp; count(tags[@ in $filters]) &gt; 0]`;
const params = { filters }

const data = client.fetch(query, params)
May 21, 2021, 6:37 PM
index.js?9101:122 Uncaught (in promise) ReferenceError: client is not defined
May 21, 2021, 6:39 PM
index.js?9101:122 Uncaught (in promise) ReferenceError: client is not defined
May 21, 2021, 6:39 PM
Sorry about that 🙂 These two lines above it:
import sanityClient from 'part:@sanity/base/client'
const client = sanityClient.withConfig({apiVersion: '2021-03-25'})
...
May 21, 2021, 6:40 PM
ah this is on the frontend app
May 21, 2021, 6:41 PM
ah this is on the frontend app
May 21, 2021, 6:41 PM
Gotcha, then just use whatever you were using before - it's about trying with the params in this case
May 21, 2021, 6:42 PM
ahh it's perfect 🙂
May 21, 2021, 6:45 PM
thank you!
May 21, 2021, 6:45 PM
🎉
May 21, 2021, 6:46 PM
that's amazing, I really appreciate your help today
user M
May 21, 2021, 6:46 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in