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.

Populate Internal Link with page object? (NON LEXICAL)

default discord avatar
nook490410 months ago
19

Looking to create navigation, that returns the object, or at least some paramteres, like the URL, for easy internal navigation. When I do this, it just returns the ID (ex. 11).



@654031862146007055
  • default discord avatar
    zed054710 months ago

    Yeah, so getting an ID back is a symptom of either insufficient access, or the data you are trying to fetch has deeply nested structure and insufficient depth param



    How are you fetching the doc

  • default discord avatar
    nook490410 months ago

    One sec, I can provide the types for eveything?



    export interface Page {


    id: number;


    title: string;


    hero: {


    links?:


    | {


    link: {


    type?: ('reference' | 'custom') | null;


    newTab?: boolean | null;


    reference?:


    | ({


    relationTo: 'pages';


    value: number | Page;


    } | null)


    | ({


    relationTo: 'posts';


    value: number | Post;


    } | null);


    url?: string | null;


    label: string;


    /**


    * Choose how the link should be rendered.


    */


    appearance?: ('default' | 'outline') | null;


    };


    id?: string | null;


    }[]


    | null;



    So this, for some reason, the type for the Page, alwayss appends a Number. (I know yorue not supposed to manually edit the types file, but even if i remove Number, it still appends it back)

  • default discord avatar
    zed054710 months ago

    This is expected behavior here

  • default discord avatar
    nook490410 months ago

    in the hero, i pull

    links

    from the props, but at that point, its already removed the entire object and replaced with a number

  • default discord avatar
    zed054710 months ago

    Types have no impact on actual data returned, it's just trying to help you by giving you type hints. The behavior is expected because Payload doesn't know in advance at what depth/access controls you are fetching with



    To know if you have a "Post"/"Page" or a number, you can do a

    typeof

    check

  • default discord avatar
    nook490410 months ago

    Heres my fetch for the home page:


    const result = await payload.find({


    collection: 'pages',


    draft,


    limit: 1,


    pagination: false,


    overrideAccess: true, // Always override access for frontend queries


    depth: 3, // Increase depth for nested relationships in blocks


    where: {


    slug: {


    equals: slug,


    },


    },


    })

  • default discord avatar
    zed054710 months ago
    const isUnpopulated = typeof doc === 'number'
  • default discord avatar
    nook490410 months ago

    ill check the type of the fetched home page



    From that fetch, the type is object. Not Page, peculiarly?

  • default discord avatar
    zed054710 months ago

    I think you are confusing TS concepts with JS



    Your populated Posts, Pages, other docs are all objects



    If, let's say, you don't have sufficient access to read those docs, they will return as number | string instead

  • default discord avatar
    nook490410 months ago

    Ah, I thought it would set the type. Okay. Im migrating from our old CMS, where can I find access?

  • default discord avatar
    zed054710 months ago

    As for your link, ensure your page/post is published and your access controls allow for them to be read



    Are you using the website template?

  • default discord avatar
    nook490410 months ago

    Yep

  • default discord avatar
    zed054710 months ago

    Nice, so your actual access functions are here:

    https://github.com/payloadcms/payload/tree/main/templates/website/src/access

    As for how they are applied to what, you would need to look into your collections, they will be placed under the

    access

    property in the config there



    How deeply nested are your links?



    Try upping your depth on that request also

  • default discord avatar
    nook490410 months ago

    is it better practice in payload not to stuff the entire object as "value", but rather spread of the slug, title, etc into the same object?

  • default discord avatar
    zed054710 months ago

    I'm not sure I follow, can you provide more info?

  • default discord avatar
    nook490410 months ago

    Wait! Just figured it out, your right, i was changing the wrong depth. I was changing that on the Hero compoentn, but not the fetched page:


    {


    "type": "reference",


    "reference": {


    "relationTo": "pages",


    "value": {


    "id": 11,


    "title": "SERVICES",


    "slug": "services"


    }


    },


    "url": null,


    }



    thats what it looks like now!

  • default discord avatar
    zed054710 months ago

    Awesome!

  • default discord avatar
    nook490410 months ago

    Thanks a ton!

  • default discord avatar
    zed054710 months ago

    That looks great



    My absolute pleasure!



    Feel free to let me know if you encounter any more blockers

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.