Hey
@1295090663544590447
where is this find happening? In a hook?
no in a custom component
In a server component right? And how did you acquire payload? Through props?
yes
Can you share the component
import React from "react";
import { UIFieldServerComponent, UIFieldServerProps } from "payload";
import { ProductVariantSelect } from "./product-variant-select";
import { Product } from "@/payload-types";
interface CustomProductVariantSelectProps extends UIFieldServerProps {
productPath?: string;
variantPath?: string;
}
export const CustomProductVariantSelect: UIFieldServerComponent = async (
props: CustomProductVariantSelectProps
) => {
const { path, payload } = props;
const products = await payload.find({
collection: "products",
depth: 1,
select: {
title: true,
variants: true,
createdAt: true,
updatedAt: true,
},
});
return (
<div
style={{
display: "flex",
flexDirection: "column",
gap: "10px",
marginTop: "10px",
marginBottom: "20px",
}}
>
<h3>Product and Variant Selection</h3>
{/* <ProductVariantSelect
path={path}
data={productsWithVariants}
productPath={props.productPath}
variantPath={props.variantPath}
/> */}
</div>
);
};When you navigate to the products collection in the ui, you have a product? Are you sure it's not a draft?
yes
Try removing the select and depth property and logging the producs you get back
it is the same empty, using related_variants i am using join, but varaints and product_varaints i am using type relationship
Sorry maybe I'm misunderstanding you, it looks like the products get populated as expected
You are getting products back
Are you saying the product
variantsdon't get populated?
yes
products are fine i mean variants
Where is the relationship field on, the product or the product variant?
product variant, i also make a relation inside product after varaints return empty but the same
Your relationship lives inside the product variant, alright, so that's why you get none back probably
You need a secondary find with a where that points to your product
The join field is not a replacement for this
The join field is more like a virtual field that simply surfaces related docs
If you want it populated from the perspective of the product, the relationship should live in the product and the join be on your variants
i know, i have relation inside product varaints that point to product, and product_variants that points to product-variant, i belive i should get the relation.
product collection
{
name: "product_variants",
type: "relationship",
hasMany: true,
relationTo: "product-variants",
},product variants
{
name: "product",
type: "relationship",
relationTo: "products",
required: true,
hasMany: false,
admin: {
description: "The product this variant belongs to",
position: "sidebar",
},
},It won't work that way if there's nothing loaded into that product_variants field, it wont populate automatically, you need to actually put docs there
When you create a variant, you need a hook or something to link the two
i am making a custom array actually inside product
{
name: "variants",
// virtual: true,
type: "array",
admin: {
description:
"Add product variants with their options and prices here",
components: {
Field:
"@/components/admin/variant/variant-component#VarinatComponent",
RowLabel:
"@/components/admin/variant/variant-label#VariantLabel",
afterInput: [
"@/components/admin/variant/variant-after-input#VariantAfterInput",
],
},
condition: (data, siblingData) => {
return Boolean(siblingData?.enableVariants);
},
initCollapsed: true,
},
// Check if the variant is not what is expected from the options and varin
validate: (
variants: any,
{ data, siblingData }: { data: any; siblingData: any }
) => {
const options = siblingData?.options || [];
const { isValid, message } = checkVariantsValidity(
variants,
options
);
return isValid ? true : message;
},
fields: [
{
name: "disabled",
type: "checkbox",
defaultValue: false,
},
{
name: "title",
type: "text",
required: true,
},
// TODO: do i need options_values, or i can replaced with values directly
// i am already in beforeOperarion hook assign the value to options_values
in the after change hook i made all the create method and it is working but i don't get it in the find
I think you are making your life more difficult than it needs to be here. I would retain one of those relationship fields depending on where you forsee the flow of data being easier to get from. It sounds like you just want to query the products collection, so a single relationship field inside your products collection with a relationTo product-variants
You assign this relationship field with variants, save, and then query it with a reasonable depth/populate
It should work just like an ordinary field
i did not have the product_varinants until i get empty varinats, i have though of multiple solution but it will include multiple api local, and i would like to minimize that, i will try with differnt approach,
and i do agree i made it some what complicated 🙂
product_varinats now have the variants, i don't know how, but glad it worked🙌
Oh nice! Glad you figured it out here
Star
Discord
online
Get dedicated engineering support directly from the Payload team.