Trying to query by not existing property Error
If you get the following error in your Medusa application's terminal:
Where X is the name of a data model. For example, LinkModel.cart. The error may also show your data model's name instead of LinkModel, such as Post.cart.
Why this Error Occurred#
This error occurs when you're using Query to filter by a linked data model's property, and Query can't apply the filter. This happens in the following cases:
- You're using a Medusa version before v2.18.0, which doesn't support filtering by linked data models.
- You passed the linked data model's filter inside a top-level
$and,$or, or$notoperator. Query passes these operators as-is to your data model's module, which doesn't recognize the linked data model's property.
For example, assuming you have a Post data model and you linked it to the Cart data model, the following filter isn't supported:
const { data } = await query.graph({
entity: "post",
fields: ["*"],
filters: {
$or: [
{
cart: {
id: "cart_123",
},
},
{
title: "My Post",
},
],
},
})
How to Fix it#
If you're using Medusa v2.18.0 or later, pass the linked data model's filter as a top-level key in filters:
const { data } = await query.graph({
entity: "post",
fields: ["*"],
filters: {
cart: {
id: "cart_123",
},
},
})
If you need to combine it with other conditions in an $or operator, run a separate query for each condition and merge the results.
If you're using a Medusa version before v2.18.0, query the link's table directly and apply the filters on its ID columns. For example:
import PostCartLink from "../links/post-cart"
// ...
const { data } = await query.graph({
entity: PostCartLink.entryPoint,
fields: ["post.*", "cart.*"],
filters: {
cart_id: "cart_123",
},
})
In the above example, you query the PostCartLink data model directly and apply the filters to the cart_id field, which holds the ID of the cart linked to the post. You'll then only retrieve the posts linked to the cart with the ID cart_123.