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.

Fetching Relationship Values

default discord avatar
_akshyalast year
22

In a collection, I’m using 2 relationship fields and 1 text field. Now, I want to create a custom slug, with all three fields. For this I need the selected values of each field. After selecting the relationship data, I can get the id of the selected item, but I need the value of it. Can you please help?

  • default discord avatar
    notchrlast year
    @897451547288567818

    Sorry for the delay!



    This seems at first like a depth issue



    Can you share how you're collection is setup?



    If you're only seeing the ID's of items, that is sometimes due to the

    depth

    property on a field

  • default discord avatar
    _akshyalast year

    {


    name: "Brand",


    type: "relationship",


    relationTo: "brand",


    required: true,


    hasMany: false,


    maxDepth: 10,


    admin: {


    width: "35%",


    },


    },



    hooks: { beforeValidate: [(data) => console.log(data)] },



    I added this hook to get the value of the selected item in the relationship

  • default discord avatar
    notchrlast year

    on the Brand field

  • default discord avatar
    _akshyalast year

    But it gets me the id only

  • default discord avatar
    notchrlast year

    add the property

    depth: 2

    and let me know if that changes anythin g

  • default discord avatar
    _akshyalast year


    Is this what you were asking?

  • default discord avatar
    notchrlast year

    Yes



    Looks like depth isn't an option

  • default discord avatar
    _akshyalast year

    Yes

  • default discord avatar
    notchrlast year
    @897451547288567818

    Someome may have a better idea, but you can access req.payload in your field hook I believe



    which will give you access to findById



    https://payloadcms.com/docs/hooks/fields
  • default discord avatar
    _akshyalast year

    hooks: {


    beforeValidate: [


    async (data) => {


    console.log(data.req.payload);


    try {


    const req = await fetch(


    ${data.req.payload.config.serverURL}/api/brand/${data.value}

    );


    const response = await req.json();


    console.log(


    "RESPONSE",


    ${data.req.payload.config.serverURL}/api/brand/${data.value}

    ,


    response


    );


    } catch (err) {


    console.log(err);


    }


    },


    ],


    },





    I am getting this error with the above code

  • default discord avatar
    notchrlast year

    Are you using a where clause



    somewhere



    OR tabs



    also, what is in the data array on that error

  • default discord avatar
    _akshyalast year

    No WHERE or TABS, I am simply using the findById the way the documentation says.


    https://payloadcms.com/docs/rest-api/overview
  • default discord avatar
    notchrlast year
    @897451547288567818

    What is your access control like on the collection brand

  • default discord avatar
    _akshyalast year

    read: publishedOnly,

  • default discord avatar
    notchrlast year

    temporarily



    change that to



    () => true



    and see if it works, then we know its an access issue

  • default discord avatar
    _akshyalast year

    Nope. Same error.

  • default discord avatar
    notchrlast year

    what does the log say for req.payload



    are you able to use a local API style command



    https://payloadcms.com/docs/local-api/overview
  • default discord avatar
    _akshyalast year

    This works!



    Thanks

    @1049775120559898725

    .

  • default discord avatar
    notchrlast year

    YAS



    @897451547288567818

    No problem 🙂

  • default discord avatar
    taun2160last year
    @1049775120559898725

    If Discord ever releases a donate button - I think you may be setting yourself up for another stream of income 😉

  • default discord avatar
    notchrlast year
    @479030528084017165

    haha thanks, but Im just happy to help

  • default discord avatar
    taun2160last year

    Legend. God bless.

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.