Can't connect Media Upload to another Collection

default discord avatar
afifaniks
last year
2 3

I have two collections. One is Product and another one is Media. As the name suggests, I want to link the Media collection with the Product collection through a relationship field in the Product collection. Even though it runs perfectly and appears on the UI but whenever I want to create a new document of Products collection I am getting an error saying - The following field value is invalid: filename. I am using @payloadcms/plugin-cloud-storage/azure for uploading media on azure which is working as expected.

Product Collection:

const Products = {
    slug: 'products',
    admin: {
      useAsTitle: 'name',
    },
    fields: [
      {
        name: 'name',
        type: 'text',
      },
      {
        name: 'price',
        type: 'number',
      },
      {
          name: 'image',
          type: 'relationship',
          relationTo: 'media',
      }
    ],
  }
  
  export default Products;

Media collection:

const Media = {
    slug: 'media',
    upload: true,
    fields: [
        {
            name: 'image',
            type: 'upload',
            relationTo: 'products',
        }
    ],
}

export default Media;

This is my buildConfig -

export default buildConfig({
  serverURL: 'http://localhost:3000',
  admin: {
    user: Users.slug,
  },
  collections: [
    Users,
    Products,
    Media
  ],
  plugins: [
    cloudStorage({
      collections: {
        media: {
          adapter: azureAdapter,
          disablePayloadAccessControl: true
        },
      },
    })
  ],
});

The issue:
image

Any help would be appreciated!

  • default discord avatar
    afifaniks
    last year

    It looks like, the issue was due to my MongoDB cluster. I deleted the collection from my db cluster and the problem was fixed!

  • default discord avatar
    sowmyapeta
    last year

    Image type in Products should be 'upload' and not 'relationship'
    {
    name: 'image',
    type: 'upload',
    relationTo: 'media',
    },

    Fields in media should be any fields which you want to describe about the image uploaded.
    fields: [
    {
    name: 'alt',
    label: 'Alt Text',
    type: 'text',
    },
    ],

    1 reply
    default discord avatar
    afifaniks
    last year

    @sowmyapeta Thank you! However, even with that, I'm still getting the same error!

  • default discord avatar
    marpstar
    9 months ago

    I just wanted to offer a solution to this error message. I encountered this error message when switching from using

    {
      name: "media",
      type: "relationship",
      relationTo: "media",
      hasMany: true,
    },
    

    to

    {
      name: "images",
      type: "array",
      fields: [
        {
          name: "image",
          type: "upload",
          relationTo: "media"
        },
      ],
    },
    

    The first of which created an index on my collection for the filename of the media field. I had to use Mongo Compass to find that index and drop it. Once I did, everything started working fine again.

Open the post
Continue the discussion in GitHub
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.