Fetching Relationship Values

default discord avatar
_akshya
3 months ago
43

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
    notchr
    3 months ago

    @_akshya 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
    _akshya
    3 months ago

    {


    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
    notchr
    3 months ago

    on the Brand field

  • default discord avatar
    _akshya
    3 months ago

    But it gets me the id only

  • default discord avatar
    notchr
    3 months ago

    add the property

    depth: 2


    and let me know if that changes anythin g

  • default discord avatar
    _akshya
    3 months ago


    Is this what you were asking?

    image.png
  • default discord avatar
    notchr
    3 months ago

    Yes



    Looks like depth isn't an option

  • default discord avatar
    _akshya
    3 months ago

    Yes

  • default discord avatar
    notchr
    3 months ago

    @_akshya 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
    _akshya
    3 months ago

    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

    image.png
  • default discord avatar
    notchr
    3 months ago

    Are you using a where clause



    somewhere



    OR tabs



    also, what is in the data array on that error

  • default discord avatar
    _akshya
    3 months ago

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


    https://payloadcms.com/docs/rest-api/overview
    image.png
  • default discord avatar
    notchr
    3 months ago

    @_akshya What is your access control like on the collection brand

  • default discord avatar
    _akshya
    3 months ago

    read: publishedOnly,

  • default discord avatar
    notchr
    3 months ago

    temporarily



    change that to



    () => true



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

  • default discord avatar
    _akshya
    3 months ago

    Nope. Same error.

  • default discord avatar
    notchr
    3 months ago

    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
    _akshya
    3 months ago

    This works!



    Thanks @notchr.

  • default discord avatar
    notchr
    3 months ago

    YAS



    @_akshya No problem šŸ™‚

  • default discord avatar
    taun2160
    3 months ago

    @notchr If Discord ever releases a donate button - I think you may be setting yourself up for another stream of income šŸ˜‰

  • default discord avatar
    notchr
    3 months ago

    @taun2160 haha thanks, but Im just happy to help

  • default discord avatar
    taun2160
    3 months ago

    Legend. God bless.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.