Hey Medusa community,
I'm working on an e-commerce project using Medusa 2.0 RC1, and I've hit a roadblock with custom fields (my own data model). I'm trying to fetch a product along with its associated custom fields, but I'm running into some issues. I've set up custom fields and can access them through the CustomFieldService, but when I try to use query.graph to get a product with its custom fields, it's not working as expected and the exact error is "Entity 'Product' does not have property 'custom_field'". I have setup the definelink and Remote link Correctly. What am I missing? Find below the sample codes FYR.
And my actual Route which is trying to fetch product along with custom_field
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
import { CUSTOM_FIELDS_MODULE } from "../../../../../modules/custom-fields"
import CustomFieldService from "src/modules/custom-fields/models/service"
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
try {
const query = req.scope.resolve(
ContainerRegistrationKeys.QUERY
)
const customFieldService: CustomFieldService = req.scope.resolve(
CUSTOM_FIELDS_MODULE
)
const { data: [product] } = await query.graph({
entity: "product",
fields: ["custom_fields.*"],
filters: {
id: req.params.id,
},
})
if (!product || !product.custom_fields) {
return res.status(404).json({ error: "Custom fields not found for this product" })
}
res.json({ product_id: product.id, product_title: product.title, custom_fields: product.custom_fields })
} catch (error) {
console.error("Error in custom fields route:", error)
res.status(500).json({ error: "Internal server error", details: error.message })
}
}