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?
@_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
{
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
on the Brand field
But it gets me the id only
add the property
depth: 2
and let me know if that changes anythin g
Is this what you were asking?
Yes
Looks like depth isn't an option
Yes
@_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
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
Are you using a where clause
somewhere
OR tabs
also, what is in the data array on that error
No WHERE or TABS, I am simply using the findById the way the documentation says.
@_akshya What is your access control like on the collection brand
read: publishedOnly,
temporarily
change that to
() => true
and see if it works, then we know its an access issue
Nope. Same error.
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
This works!
Thanks @notchr.
YAS
@_akshya No problem š
@notchr If Discord ever releases a donate button - I think you may be setting yourself up for another stream of income š
@taun2160 haha thanks, but Im just happy to help
Legend. God bless.
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.