Unsure about depth - added upload/Media relationship to Users and not getting URL via Local API

default discord avatar
johnrisby
last month
6

I've added a profileImage field to users which is a simple upload type with a relationship to media.



It works, I can add one and when I get a post it is returned, but only the ID of the media. I've tried setting the depth when getting the author, but it still doesn't give me the url of the media (which I can get from the images on the meta field of an article, for instance).



Obviously I can get it with another call but I'd rather not if i can avoid it.



What am I doing wrong please? Thanks!

  • discord user avatar
    tylandavis
    Payload Team
    last month

    Hi @johnrisby, have you set the Access Control for your

    media

    collection?

  • default discord avatar
    johnrisby
    last month

    hi, I'm using the Local API and it says "In the Local API, all Access Control functions are skipped by default, allowing your server to do whatever it needs. But, you can opt back in by setting the option overrideAccess to false."

  • discord user avatar
    tylandavis
    Payload Team
    last month

    Gotcha, can you add a code sample of your request?

  • default discord avatar
    johnrisby
    last month

    sure, currently it's


     
    const post:Post = await payload.findByID({
        collection: "posts", 
        id: "64d0fb2fb5333910aceb33eb", 
        depth: 4,
        // locale: "en",
        // overrideAccess: false,
        // showHiddenFields: true,
      });


    I've used various depth levels (and none, which should be the default of 10 I think?)



    if I proceed to do this



    const author:User = post.author as User;
    
      const authorDetail:User = await payload.findByID({
        collection: "users", // required
        id: author.id,
        depth: 2,
        // locale: "en",
        // overrideAccess: false,
        // showHiddenFields: true,
      });
    
      const profileImage:Media = await payload.findByID({
        collection: "media", // required
        id: author.profileImage as string,
        depth: 2,
        // locale: "en",
        // overrideAccess: false,
        // showHiddenFields: true,
      });

    `



    it works and profileImage has the sizes / urls etc but ...



    (just putting my daughter to bed, be back in 5 or 10). thanks

  • discord user avatar
    tylandavis
    Payload Team
    last month

    Your request looks correct to me. The only other thing I can think of is a

    maxDepth

    value set on your

    media

    config, but I imagine you'd have thought of that already.



    If you set

    read

    access to true on your

    media

    collection, does it make a difference?

  • default discord avatar
    johnrisby
    last month

    the access for all operations are true, but I turned read to false as a test and it still worked (and didn't work in the case of the profileImage) 🙂



    I have checked for any places I can use maxDepth but I only started with payload a couple of day sago so I may be missing something?



    Also, I should say I've got it set up like this:

    https://discord.com/channels/967097582721572934/1138020824520069150

    tl:dr - payload is running on the same server as my next.js app that I'm pulling the data in to, but not fully combined



    For some reason this is now working...? The only thing I can think of - but I'm 99% sure I'd done this - is the way I'm connecting the two instances (next.js app running separately from a payload app on the same server) means that the next.js app is having to import the compiled js for the typedefs. So, I think it could make sense that if I hadn't run yarn build (and was just using yarn dev) the typedef wouldn't have been updated...?



    But I am pretty sure I did that a few times during the debugging.



    Still, I woke up this morning and was hit by an immediate error trying to cast the object ID of the profile image... when I looked in to it, it was because the data being returned now contained all the data I wanted... So, all's well that ends well 😉 (although I'd still like to be 100% sure WHY! 😉 ) .



    Thanks again!

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.