
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeLooking at your code, the issue is likely that you're trying to access nested properties that may not exist yet when the filter function runs. Here are a few things to check:
The Problem:
The document object in filter functions represents the current document being edited, so document.content?.product?._ref should work, but you need to ensure the document structure matches and that you're filtering correctly.
Solution:
Based on the Sanity documentation on reference filters, here's a corrected version:
filter: ({ document }) => {
const productRef = document?.content?.product?._ref
// Make sure we have a product reference before applying filter
if (!productRef) {
return {} // Return empty object to show all results when no product selected
}
return {
filter: 'product._ref == $productRef',
params: {
productRef: productRef,
},
}
}Key points to understand:
{} when there's no filter to apply (not undefined or nothing)product to productRef in params to be clearer, but the actual name doesn't matter as long as it matches in both the filter string and params objectDebugging tips:
Since you can't use console.log inside the filter function, try these approaches:
content.product actually exists in your documentproduct reference field that you're filtering againstIf you're still having issues, it would help to know:
to field)?product reference field?Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store