Additional added fields in Media object API are not returned

default discord avatar
loaialsharee11 months ago
3

Hi! I am having a collection that has an upload field. When I use the API endpoint to get the collection data including the media object related, I only receive the media object with the following fields by default:

id

,

filename

,

mimeType

,

filesize

,

width

,

height

,

url

. However, all additionally added fields using

beforeChange

hook in media collection are not returned even though they are successfully stored in media db. Is there anything I need to enable/configure to be able to return them as well?

  • default discord avatar
    imcorfitz11 months ago

    How does the object in MongoDB look?

  • default discord avatar
    loaialsharee11 months ago

    @imcorfitz It looks like this:



    _id: ObjectId('id')
    external_url: "test"
    filename: "test-18.png"
    mimeType: "image/png"
    filesize: 4369
    width: 248
    height: 71
    __v: 0

    I need to get the

    external_url

    field to be part of the returned API.



    Update: When I put the endpoint within the collection itself, that field appears, but when I put the endpoint in

    payload.config.ts

    , the field disappears. That is even more confusing because I intent to place the endpoint within

    payload.config.ts

    not within the collection



    Update2: It seems that my issue was that I have set the field directly to

    hidden: true

    thinking that it will disable the field from appearing on the UI, while the reality is that it was actually hiding the field from appearing in the API.


    {
          name: 'external_url',
          type: 'text',
          hidden: true
    }

    The correct implementation for hiding the field from admin UI is that it should be wrapped with admin config like this:


    {
          name: 'external_url',
          type: 'text',
          admin: {
            hidden: true,
          },
    }

    This would hide it from admin UI but keep it in the API. Thankss!!

  • default discord avatar
    imcorfitz11 months ago

    🚀

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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