#Help Needed: Fetching Product <> Custom Fields (my custom data model) in Medusa 2.0 RC1

22 messages · Page 1 of 1 (latest)

cloud current
#

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 })
}
}

#

Just additional information, I've setup Definelink and RemoteLink per the docs

import { defineLink } from "@medusajs/framework/utils"
import ProductModule from "@medusajs/medusa/product"
import CustomFieldsModule from "../modules/custom-fields"

export default defineLink({
linkable: ProductModule.linkable.product,
},
{
linkable: CustomFieldsModule.linkable.customField,
isList: true,
}
)

import {
createStep,
StepResponse,
} from "@medusajs/framework/workflows-sdk"
import {
Modules,
ContainerRegistrationKeys,
} from "@medusajs/framework/utils"
import { CUSTOM_FIELDS_MODULE } from "../../../modules/custom-fields"

type LinkCustomFieldToProductStepInput = {
productId: string
customFieldId: string
}

export const linkCustomFieldToProductStep = createStep(
"link-custom-field-to-product",
async ({ productId, customFieldId }: LinkCustomFieldToProductStepInput, { container }) => {
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

await remoteLink.create({
  [Modules.PRODUCT]: {
    product_id: productId,
  },
  [CUSTOM_FIELDS_MODULE]: {
    custom_field_id: customFieldId,
  },
})

return new StepResponse(undefined, {
  productId,
  customFieldId,
})

},
async ({ productId, customFieldId }, { container }) => {
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)

await remoteLink.dismiss({
  [Modules.PRODUCT]: {
    product_id: productId,
  },
  [CUSTOM_FIELDS_MODULE]: {
    custom_field_id: customFieldId,
  },
})

}
)

tawdry marlin
#

Can you share a reproducible repository please

cloud current
tawdry marlin
#

do you have something other than .rar? for example .zip?

cloud current
#

sure, I can do zip as well

#

one min

#

I've uploaded the zip file in the same drive folder

#

let me know if you can access fine. Thanks for your help!

tawdry marlin
#

yes I can, I ll have a look tomorrow probably, I am on something right now

cloud current
#

Sure, thanks @tawdry marlin

cloud current
#

Hey @tawdry marlin, sorry to bug you do we have any update on this? I took the latest Rc2 as well and I see the same problem so definitely there is some issue with query.graph

lavish parcel
#

You created the link as list, have you tried custom_fields.* ? plural

cloud current
#

the underlying data model name is custom_field anyways so not sure why I should refer custom_fields but still I tried per your reco and here is the error 'Method "listCustomFields" does not exist on "customFieldsService"'

lavish parcel
#

because you set your link with isList: true

cloud current
#

oh okay, thats interesting so that means I need to refer Plural then. Now per the docs I understand my CustomFieldService since it extends MedusaService, it should automatically create the necessary CRUD + other method like list LIST and Retrieve, I dont understand why its raising that as an error in this case then

#

when I try to invoke listMethods directly on the CustomFieldService it works flawlessly its happening only with query.graph

lavish parcel
#
class CustomFieldService extends MedusaService({
  model: CustomField,
}) {

}

you are naming it model, so it tries to create listModels.

it should be:

class CustomFieldService extends MedusaService({
  CustomField,
}) {

}

then you'll have listCustomFields

cloud current
#

you know what, that exactly was the issue... Thank you so much! Love the product guys and more love to your team for their awesome support! ❤️

lavish parcel
cloud current
#

Awesome, thanks for letting me know! doing that right away...

tawdry marlin
#

Sorry that i haven’t been able to participate further 🙏thanks carlos. I was super busy lately my bad