Search code examples
javascriptnuxt.jsstrapi

Put request in strapi


I have a table of users, a user has favorite items, one user can have many items. In the admin panel, I can freely add/remove items, but how can I do this in the form of a request?

I am interested in the mechanism itself, the process of adding and removing elements. I have the necessary ID to delete and add. How to write correctly?

I need a PUT request

async updateFavorites(id, status) {
    let {
        data,
        error
    } = await useFetch(
        `${useRuntimeConfig().env.STRAPI_URL}/api/users/${this.user.id}`, {
            method: "PUT",
            headers: {
                Authorization: `Bearer ${this.jwt}`,
                "Content-Type": "application/json",
            },
            body: {

            }
        }
    );
}


Solution

  • To add a new element to the array, I just added a new id to the array, strapi picked up the id itself and found a match. If there is a relationship with another element in the table, it is enough to add an id.

     
     this.user.Favorites.push(id);
            let { data, error } = await useFetch(
              `${useRuntimeConfig().public.strapi.url}/api/users/${this.user.id}`,
              {
                method: "PUT",
                headers: {
                  Authorization: `Bearer ${cookie.value}`,
                  "Content-Type": "application/json",
                },
                body: {
                  Favorites: this.user.Favorites,
                },
              }
            );