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.

How to update a collection's upload field?

default discord avatar
.wszlast year

Hi !



I'm struggling to upload an image to the

upload field

of my collection via

payload.update()

in an

afterChange

Hook


I couldn't find any examples in the docs, and this part is invalid, but have no idea how to fix it



Any pointers appreciated !


Thanks



Invalid update field :


image: path.resolve(__dirname, filePath)

Error


[15:04:55] ERROR (payload): ValidationError: The following field is invalid: image


Here is the

payload.update

logic


const post = await payload.update({
     collection: "pdfs", // required
     id: doc.id, // required
     data: {
        title: "Updated Title",
        alt: "Updated Alt",
        image: path.resolve(__dirname, filePath),
       },
     depth: 2,
     locale: "en",
     user: "Admin",
     overrideAccess: true,
     showHiddenFields: true,
     context: {
      // set a flag to prevent from running again
       triggerAfterChange: false,
      },
  });



The array field in my pdf collection


{
     name: "image",
     type: "upload",
     relationTo: "images",
},
  


How to update a collection's upload field?



hi so I eventually understood that i had to create the image before updating ! duh



    const filePath = '/home/node/app/src/uploads/pdfs/' + doc.filename + 0 + '.png';
    fs.writeFileSync(filePath, pngPage[0].content);
    payload.logger.info("Generate pdf images success");
    const createdImage : Image = await payload.create({
        collection: "images", // required
        data: {
           title: "title",
       alt : "alt"
        },
        locale: "en",
        user: "Admin",
        overrideAccess: true,
        showHiddenFields: false,
        disableVerificationEmail: true,
        filePath: path.resolve(__dirname, filePath),
    });


    const post = await payload.update({
        collection: "pdfs", // required
        id: doc.id, // required
        data: {
            title: "Updated Title",
            alt: "Updated Alt",
            img: createdImage.id,
        },
        depth: 2,
        locale: "en",
        user: "Admin",
        overrideAccess: true,
        showHiddenFields: true,
        context: {
            // set a flag to prevent from running again
            triggerAfterChange: false,
        },
    });



it kind of works, but at first document save i get

app-payload-1  | TypeError: Cannot read properties of null (reading 'toJSON')
app-payload-1  |     at deleteByID (/home/node/app/node_modules/payload/src/collections/operations/deleteByID.ts:111:30)
app-payload-1  |     at processTicksAndRejections (node:internal/process/task_queues:95:5)
app-payload-1  |     at async Promise.all (index 0)
app-payload-1  | [nodemon] app crashed - waiting for file changes before starting...


I don't understand what's wrong? what's triggering the delete operation?



it was caused by a hook in my images collections

    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.