User I am facing an issue with my application's endpoint for adding a like to a post. The endpoint is supposed to receive the user id who liked the post and insert it, along with the number of likes (not crucial at this moment), into a database. To achieve this, I first retrieve all the likes associated with a post using the following code snippet:
const likes = await strapi.db.query("api::post.post").findOne({
where: {id: postId},
populate: [
"likes"
]
});
The output of this code looks something like this (although not exactly, as I need to extract this part for use in the endpoint):
[
{ id: 8, no_of_likes: 21 },
{ id: 10, no_of_likes: 13 },
{ id: 11, no_of_likes: 16 }
]
Next, I append a new entry to this array using push, resulting in a new JSON array that resembles this:
[
{ id: 8, no_of_likes: 21 },
{ id: 10, no_of_likes: 13 },
{ id: 11, no_of_likes: 16 },
{ id: 2, no_of_likes: 22 }
]
Afterwards, I attempt to update the likes field in my database with this modified array as follows:
//Add user to the liked_by section.
const entry = await strapi.entityService.update("api::post.post", postId, {
data: {
likes: likesJsonObject
}
});
However, I encounter the error message stating "Some of the provided components in likes are not related to the entity," even though the id 2 does exist within my users. Interestingly, manually adding the same record ({id: 2, no_of_likes: 21}) through the Strapi content manager works perfectly fine. How can I resolve this issue programmatically via the endpoint?