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.

PATCH one object in a row field

default discord avatar
jonatanh2 years ago
14

I am wondering if it is possible to do a PATCH request against the API to update just one object in a row field.



Currently when providing id of the object all other objects are deleted on the request. Do I have to include all objects in the row?

  • default discord avatar
    notchr2 years ago
    @394931100465496064

    Morning! Could you show us a code example of how you're trying to update the row?

  • default discord avatar
    jonatanh2 years ago

    Not clean but...



        function updateJob(
            task = {},
            url = 'http://localhost:3000/api/projects/65994cc79a2e917a96e2787d',
            data = {}
        ) {
            console.log(task);
            let from = new Date(task.task.model.from);
            let to = new Date(task.task.model.to);
            let printer = task.task.model.resourceId;
            let id = task.task.model.id;
            // Default options are marked with *
            const response = fetch(url, {
                method: 'PATCH', // *GET, POST, PUT, DELETE, etc.
                mode: 'cors',
                headers: {
                    'Content-Type': 'application/json'
                    // 'Authorization': 'Bearer YOUR_TOKEN_HERE', // If you need an authorization header
                },
                body: JSON.stringify({
                    printers: printer,
                    prints: [
                        {
                            id: id,
                            start: from,
                            end: to,
                            title: task.task.model.title
                        }
                    ]
                })
            });
            console.log(response);
            return response;
        }
  • default discord avatar
    notchr2 years ago
    @394931100465496064

    Hmm, so which record are you updating



    I see you pass a printers object and a prints array

  • default discord avatar
    jonatanh2 years ago

    ´The goal is to update the specific object in the prints array. When I run this patch it removes removes all other objects in the prints array except for the one I specify

  • default discord avatar
    notchr2 years ago

    Ah, and you dont have the existing prints value



    otherwise I would say, spread that into the call



    Is it a relationship

    @394931100465496064
  • default discord avatar
    jonatanh2 years ago

    No it is not



    Maybe it should be?

  • default discord avatar
    notchr2 years ago

    well, print is an array type



    field



    right

  • default discord avatar
    jonatanh2 years ago

    I could get all print objects but this seems nicer



    Correct

  • default discord avatar
    notchr2 years ago

    So there may be another way, but this is my best guess



    1.) Get the current value of the array, spread the existing value + new value



    2.) Change the field type to a relationship, create your array items as collection documents



    Only because



    In scenario one, it doesn't seem to let you patch an individual array item otherwise



    But with relations, you can modify a specific entry I believe



    However, relations are generally stored as ID's so you would need to fetch the related document anyway



    Let me ping

    @858693520012476436

    , in case he knows some trickery



    But I think these are your options

  • default discord avatar
    jonatanh2 years ago

    That is fine, was hoping I could patch one object if I specified the id.



    I already have all objects in the frontend but just updating data for the object that actually gets updated feels better that updating everything all the time

  • default discord avatar
    notchr2 years ago

    I agree with you, however I don't think the array dats is represented in a way where you can "find one"

  • default discord avatar
    paulpopus2 years ago

    Yeah without a custom endpoint i dont think this is currently possible that way

  • default discord avatar
    notchr2 years ago

    Ah I didn't consider a custom endpoint, that could work if its really necessary

  • default discord avatar
    jonatanh2 years ago

    It is not, thanks for the clarification!



    Will jump to the next problem 😄

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.