Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Troubleshooting Empty Array with Payload Find Method

default discord avatar
zed0547last year
20

Hey

@1295090663544590447



where is this find happening? In a hook?

  • default discord avatar
    hus200salast year

    no in a custom component

  • default discord avatar
    zed0547last year

    In a server component right? And how did you acquire payload? Through props?

  • default discord avatar
    hus200salast year

    yes

  • default discord avatar
    zed0547last year

    Can you share the component

  • default discord avatar
    hus200salast year
    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>
      );
    };
  • default discord avatar
    zed0547last year

    When you navigate to the products collection in the ui, you have a product? Are you sure it's not a draft?

  • default discord avatar
    hus200salast year

    yes

  • default discord avatar
    zed0547last year

    Try removing the select and depth property and logging the producs you get back

  • default discord avatar
    hus200salast year

    it is the same empty, using related_variants i am using join, but varaints and product_varaints i am using type relationship

  • default discord avatar
    zed0547last year

    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

    variants

    don't get populated?

  • default discord avatar
    hus200salast year

    yes



    products are fine i mean variants

  • default discord avatar
    zed0547last year

    Where is the relationship field on, the product or the product variant?

  • default discord avatar
    hus200salast year

    product variant, i also make a relation inside product after varaints return empty but the same

  • default discord avatar
    zed0547last year

    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

  • default discord avatar
    hus200salast year

    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",
          },
        },
  • default discord avatar
    zed0547last year

    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

  • default discord avatar
    hus200salast year

    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

  • default discord avatar
    zed0547last year

    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

  • default discord avatar
    hus200salast year

    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🙌

  • default discord avatar
    zed0547last year

    Oh nice! Glad you figured it out here

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.