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?
Morning! Could you show us a code example of how you're trying to update the row?
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;
}Hmm, so which record are you updating
I see you pass a printers object and a prints array
´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
Ah, and you dont have the existing prints value
otherwise I would say, spread that into the call
Is it a relationship
@394931100465496064No it is not
Maybe it should be?
well, print is an array type
field
right
I could get all print objects but this seems nicer
Correct
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
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
I agree with you, however I don't think the array dats is represented in a way where you can "find one"
Yeah without a custom endpoint i dont think this is currently possible that way
Ah I didn't consider a custom endpoint, that could work if its really necessary
It is not, thanks for the clarification!
Will jump to the next problem 😄
Star
Discord
online
Get dedicated engineering support directly from the Payload team.