Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Multipart Form-Data Upload Fails with 'Cannot read properties of undefined (reading 'firstName')' in Tests Collection #11397

Open
alireza-asgharii opened this issue Feb 26, 2025 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@alireza-asgharii
Copy link

Description:
I’m experiencing an issue when trying to create a document in the tests collection via REST API using multipart/form-data to upload an image along with text fields (firstName, lastName, user). The request works fine when using JSON with just the media ID, but fails with the error Cannot read properties of undefined (reading 'firstName') when I send a file directly.

Steps to Reproduce:

  1. Set up a tests collection with the following configuration:
    import { type CollectionConfig } from "payload";
    
    export const Tests: CollectionConfig = {
      slug: "tests",
      fields: [
        { name: "firstName", type: "text" },
        { name: "lastName", type: "text" },
        { name: "user", type: "relationship", relationTo: "users" },
        { name: "image", type: "upload", relationTo: "media", required: true },
      ],
    };
    
    

*** I have this collection and I want to make a post request with the api/test path.
I did the test in my postman:
1- Body: If I send the data in the body format, i.e. as follows, the request is done correctly (in this method, the file is uploaded first and its ID is placed in the request)
{
"lastName": "test new",
"firstName": "me",
"image": "67bed87ed05f4b76c53e2bde"
}

2- form-data: If I want to send the request in the multiform method, i.e. send the file, the server side returns an error:

Req:
Image
err:
Cannot read properties of undefined (reading 'firstName')

Does the payload have a method for what I want (sending a file to the desired collection) or not?

@alireza-asgharii alireza-asgharii added the documentation Improvements or additions to documentation label Feb 26, 2025
@PatrikKozak PatrikKozak self-assigned this Feb 26, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants