How to update component in user collection

System Information
  • Strapi Version: 4.5.5
  • Operating System: Windows
  • Database: MySQL
  • Node Version: 14.20
  • NPM Version: 8.18.0
  • Yarn Version: 1.22.19

Hi there, I’m trying to updata a component called “billingAddress” within my user collection. I have set up a route to be able to enable a user to update their own data based on this video: Updating Your Own User Info in Strapi - YouTube

I’m able to update user data but once I need to update data in the component I’m not able to update any data.

This is what my extension looks like on the strapi backend:

module.exports = (plugin) => {
    plugin.controllers.user.updateMe = async (ctx) => {
        if (!ctx.state.user || !ctx.state.user.id) {
            return ctx.response.status = 401;
        }
        await strapi.query('plugin::users-permissions.user').update({
            where: { id: ctx.state.user.id },
            data: ctx.request.body,
        }).then((res) => {
            ctx.response.status = 200;
        })
    }

    plugin.routes['content-api'].routes.push(
        {
            method: "PUT",
            path: "/user/me",
            handler: "user.updateMe",
            config: {
                prefix: "",
                policies: []
            }
        }
    )

    return plugin;
}

This is the Axios put request I’m using to update the user data from the frontend:

const handleUpdateBillingAddress = () => {
        axios.put('http://localhost:1337/api/user/me', {
            billingAddress: {
                zipCode: "2840",
                id: 1,
                firstName: "Tim",
                lastName: "kerrem",
                company: "mycompany",
                address1: "mystreet 42",
                address2: null,
                city: null,
                country: "Belgium",
                provinceOrState: null,
                zipCode: null,
                phone: "+31412412412",
                email: null
            }
        },

        {
            headers: {
                'authorization': `Bearer ${jwt}`,
                'Content-Type': 'application/json'
            },
            },
        )
        .then(response => {     
            console.log(response)
            notification.open({
                type: 'success',
                message: 'Success!',
                description:'Your user information has been updated',
                });
            })
        .catch(error => {
            console.log(error);
            console.log('An error occurred:', error.response);
            notification.open({
                type: 'error',
                message: 'Something went wrong',
                description:'Some of your credentials are not valid',
                });
        });
    }

Would be really helpful if someone could advise me on how to update the component

1 Like

Instead of strapi.query.update use strapi.entityService.update
Example :-

    await strapi.entityService.update('plugin::users-permissions.user', ctx.state.user.id, {
      data: ctx.request.body
    }).then((res)=>{
      ctx.response.status = 200;
      ctx.response.body = res
    })
2 Likes

Thanks, this resolved my issue indeed :+1:

thank you!

Example on how to directly update via the DB connection in a beforeUpdate:

 async beforeUpdate(data) {
    console.dir(data, { depth: null, colors: true });

    if (!data?.params?.data?.seo) {
      return;
    }

    const structuredData = await generateProductJsonLd(data);

    await strapi.db
      .connection("components_shared_seos")
      .where("id", data.params.data.seo.id)
      .update({
        structured_data: structuredData,
      });
  },

You can get the name of the table by connecting to the DB and search the name of your component there. This example shows a generation an auto generation of JSON-LD for SEO