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.

Clearing a relationship field

default discord avatar
Deleted User3 years ago
8

When clearing a relationship field with required: false I get the error


"TypeError: Cannot use 'in' operator to search for 'relationTo' in null"



Is this a bug?

  • default discord avatar
    jessrynkar3 years ago

    Hi

    @456226577798135808

    , I'm not able to replicate this error - is this happening across all relationship fields or just one? If you could share your code for the relationship field causing the error that would be helpful

  • default discord avatar
    Deleted User3 years ago

    1. In The payload demo, select a post.


    2. Then select Internal link and fill out the Document to link to,


    3. Save the Document.


    4. Clear the "Document to link to"


    5. Select the option Custom URL


    6. Save the Document



    Throws "TypeError: Cannot use 'in' operator to search for 'relationTo' in null"



    import { CollectionConfig } from "payload/types";


    export const Pages: CollectionConfig = {


      slug: "pages",


      labels: {


        singular: "Page",


        plural: "Pages",


      },


      admin: {


        useAsTitle: "title",


      },


      fields: [


        {


          name: "title",


          label: "Title",


          localized: true,


          type: "text",


          required: true,


        },


        {


          name: "related",


          label: "Related",


          type: "relationship",


          relationTo: ["posts" /

    "articles"

    /],


          hasMany: false,


          required: false,



          admin: {


            readOnly: false,


          },


        },


      ],


    };



    I guess it's when using an array with relationTo for many relationships

  • default discord avatar
    jessrynkar3 years ago

    This error is occurring when

    hasMany: false

    and

    relationTo

    is an array - I'll relay this back to the team! For now, could you set

    hasMany

    to

    true

    and let me know if that works for you?

  • default discord avatar
    Deleted User3 years ago

    it works if hasMany: true 🙂

  • default discord avatar
    jessrynkar3 years ago

    Great - use that for now and we will get the issue fixed soon. Thanks for catching this! 🙌

  • default discord avatar
    Deleted User3 years ago

    When localized is true on relationships, it also fails if hasMany is false.

  • default discord avatar
    jessrynkar3 years ago

    Hey just an update, a fix will be merged and released later on today

  • default discord avatar
    Deleted User3 years ago

    Works like a charm 🍻

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.