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 an upload field via the REST Api when it is inside a group field?

default discord avatar
notchrlast year
24

Hello all!



I'm attempting to update a group field on a collection, the group consists of upload fields.



I have a "documents" upload-enabled collection, and I've tested uploading files to it via REST and that works as expected.


I then want to update my "example" collection which has a "forms" field that is of the type Group.



So first I make a call to upload the file, that returns the document in the response.


I then attempt to make a PATCH request to a specific doc in my Example collection.



From what I can see, the body needs to be the part of the collection that should update, so in my instance, I've tried the following...




    // Error
    let obj: any = {};
    obj['forms.dealerLicense'] = doc.id;

    // Error
    let obj: any = {};
    obj['forms.dealerLicense'] = doc;

    // Error
    let obj: any = {
      forms: { dealerLicense: doc.id }
    };


I either get a 400 error, or I get

[{message: "Updating the path 'forms.dealerLicense' would create a conflict at 'forms'"}]

As a note, forms would eventually be like...



forms: {a: '', b: ''}


When I inspect the dev tools for what patch the admin panel does, it looks the same as

forms: { dealerLicense: doc.id }


@jarrod69420 😄



The relevant portion in the PATCH issued by the admin panel has:



"forms":{"dealerLicense":"648c58b68962b04a9f60ddf9"}


so I know I need to pass the doc.ID, but it still claims it would create a conflict at 'forms'



(when I try to do my own patch)

  • default discord avatar
    jarrod69420last year

    can you paste your related field config

  • default discord avatar
    notchrlast year

    sure thing



    Example Collection


    This is the "forms" field group on my example collection
    
     {
          name: "forms",
          label: "Forms / Documents",
          type: "group",
          fields: [
            {
              name: "dealerLicense",
              label: "Dealer License",
              type: "upload",
              relationTo: "dealer-documents",
              required: false,
            },
            {
              name: "exampleb",
              type: "upload",
              relationTo: "dealer-documents",
              required: false,
            },
            {
              name: "examplec",
              type: "upload",
              relationTo: "dealer-documents",
              required: false,
            },
            {
              name: "exampled",
              type: "upload",
              relationTo: "dealer-documents",
              required: false,
            },
          ],
        },


    Upload Enabled collection


    const DealerDocuments: CollectionConfig = {
      slug: "dealer-documents",
      access: {
        read: () => true,
        update: () => true,
        create: () => true,
        delete: () => true,
      },
      upload: {
        staticURL: "/payload/uploads/dealer-documents",
        staticDir:
          process.env.NODE_ENV === "production"
            ? process.env.PAYLOAD_PUBLIC_UPLOAD_PATH_PROD
            : process.env.PAYLOAD_PUBLIC_UPLOAD_PATH_DEV,
        imageSizes: [],
        adminThumbnail: "thumbnail",
        mimeTypes: ["application/pdf", "image/*"],
      },
      fields: [
        {
          name: "dealerId",
          label: "Dealership ID",
          type: "text",
          required: true,
        },
      ],
    };


    Client method to update Example collection with uploaded doc


      public updateDealerForms(token: string, docId: string, doc: DealerDocument) {
      // Note: docId was being used as the key for obj.forms (e.g. "dealerLicense") but I'm manually setting it here
        let obj: any = {};
        obj['forms.dealerLicense'] = doc.id;
    
        return this.http
          .patch(this.apiRoot + 'dealerships/' + token, obj)
          .pipe(
            catchError((error: HttpErrorResponse) =>
              throwError(() => new Error(error.message))
            )
          );
      }


    Client method to upload document


      /** Dealership Application - Document Uploads */
      public uploadDealerDocument(token: string, file: File) {
        const formData = new FormData();
        formData.append('dealerId', token);
        formData.append('file', file);
    
        return this.http
          .post<{ message: string; doc: DealerDocument }>(
            this.apiRoot + 'dealer-documents',
            formData
          )
          .pipe(
            catchError((error: HttpErrorResponse) =>
              throwError(() => new Error(error.message))
            )
          );
      }
  • default discord avatar
    jarrod69420last year

    hmmm, well to start I do think you should be sending your data in object form like you showed in your attempts above


    {
      forms: {
        dealerLicense: doc.id
      }
    }
  • default discord avatar
    notchrlast year

    @jarrod69420 When I try that format, I get

     BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer


    wait maybe not



    IT LIVES



    not sure what i did differently though



    :X

  • default discord avatar
    jarrod69420last year

    so it is working?

  • default discord avatar
    notchrlast year

    Yes @jarrod69420 - maybe it was how I shaped my object, but it seems to be OK now



    Thank you 🙂

  • default discord avatar
    jarrod69420last year
    NICE
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.