Querying for arrays in arrays in Sanity.io
33 replies
Last updated: May 21, 2021
M
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?
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
P
Querying for arrays in arrays is not supported unfortunately. However, the funny thing is that it's actually possible 🙂
Credits to
Credits to
user E
:*[ _type == "project" && count(tags[@ in ["I Know the End", "themeTwo"]]) > 0 ]
May 21, 2021, 6:13 PM
M
ah!
May 21, 2021, 6:14 PM
M
wonderful
May 21, 2021, 6:14 PM
M
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(<unknown>, string)\",\n \"type\": \"queryParseError\"\n }\n}" statusCode: 400 message: "Invalid function call: in(<unknown>, string)" name: "ClientError" stack: "ClientError: Invalid function call: in(<unknown>, 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.<computed> (webpack-internal:///./node_modules/get-it/lib/request/browser-request.js:77:5)" _proto_: BaseError
May 21, 2021, 6:17 PM
M
M
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
P
It misses the array brackets around 'I know the End'. This should work: https://owp2y0vr.api.sanity.io/v1/data/query/production?query=*%5B_type%20%3D%3D%20%22pr[…]%20in%20%5B%27I%20Know%20the%20End%27%5D%5D)%20%3E%200%5D
May 21, 2021, 6:25 PM
M
ah OK, thanks
user M
- wondering how I can get this working with a dynamic arrayMay 21, 2021, 6:26 PM
P
Could you filter beforehand to leave only the string type items?
May 21, 2021, 6:28 PM
M
yeh, I've just mapped it to a new array - but it's still not liking it
May 21, 2021, 6:29 PM
M
const query = groq`*[_type == "project" && count(tags[@ in '${filters}']) > 0]`;
May 21, 2021, 6:29 PM
M
thanks so much for your help
user M
May 21, 2021, 6:29 PM
M
feel like I'm close!
May 21, 2021, 6:29 PM
M
works perfectly when I copy and paste the exact console log
May 21, 2021, 6:31 PM
M
works perfectly when I copy and paste the exact console log
May 21, 2021, 6:31 PM
M
just get various errors "Undefined function \"the\"" etc
May 21, 2021, 6:34 PM
M
just get various errors "Undefined function \"the\"" etc
May 21, 2021, 6:34 PM
P
Have you tried without the single quotes?
const query = groq`*[_type == "project" && count(tags[@ in ${filters}]) > 0]`;
May 21, 2021, 6:35 PM
P
Have you tried without the single quotes?
const query = groq`*[_type == "project" && count(tags[@ in ${filters}]) > 0]`;
May 21, 2021, 6:35 PM
M
yeh, lots of combinations
May 21, 2021, 6:35 PM
M
that gives me the above error
May 21, 2021, 6:36 PM
P
What happens if you do something like this?
const query = `*[_type == "project" && count(tags[@ in $filters]) > 0]` const params = { filters } const data = client.fetch(query, params)
May 21, 2021, 6:37 PM
P
What happens if you do something like this?
const query = groq`*[_type == "project" && count(tags[@ in $filters]) > 0]`; const params = { filters } const data = client.fetch(query, params)
May 21, 2021, 6:37 PM
M
index.js?9101:122 Uncaught (in promise) ReferenceError: client is not defined
May 21, 2021, 6:39 PM
M
index.js?9101:122 Uncaught (in promise) ReferenceError: client is not defined
May 21, 2021, 6:39 PM
P
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
M
ah this is on the frontend app
May 21, 2021, 6:41 PM
M
ah this is on the frontend app
May 21, 2021, 6:41 PM
P
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
M
ahh it's perfect 🙂
May 21, 2021, 6:45 PM
M
thank you!
May 21, 2021, 6:45 PM
P
🎉
May 21, 2021, 6:46 PM
M
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.