Skip to content

Media collection's url field is not populated when using defaultPopulate #10495

Closed Answered by igor-zmitrovich
igor-zmitrovich asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, the issue is that the filename was not populated.

When I add the filename: true to defaultPopulate it works.

import type { CollectionConfig } from "payload";

export const Media: CollectionConfig = {
  ...
  defaultPopulate: {
    alt: true,
    url: true,
    filename: true,  // Required for `url`, otherwise it will be `null`
  },
  ...
};
console.log(firstPage.docs[0]);
>> {
  id: 22,
  name: 'Rice',
  slug: 'rice',
  image: {
    id: 26,
    alt: 'Rice',
    url: '/api/media/file/rice.webp',
    filename: 'rice.webp',
  },
  updatedAt: '2024-12-29T16:17:33.301Z',
  createdAt: '2024-12-29T16:17:18.892Z'
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by igor-zmitrovich
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant