如何在 Strapi 中更新用户

How to update user in Strapi

在 Strapi 中,每个用户定义的集合类型都有一个允许 create/find/update/etc 的默认服务。在相应的模型上。 例如,Strapi 控制器中的以下代码将使用给定数据更新账单收集类型:

await strapi.services.bill.update({id}, {verified: true, receipt_number})

但是 User 内置集合类型没有服务。 我需要通过自定义控制器更改用户的角色。

好的。我找到了解决方案。我们可以修改或创建自定义服务,并从此处的核心服务实现中获得灵感: https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services

对于我的情况,我已将此方法添加到自定义服务中:

updateUserRole: async (user_id, role_id) => {
return await strapi.query('user', 'users-permissions').update({id: user_id}, {role: role_id});}

现在我可以像这样访问此服务了:

await strapi.services.customServiceName.updateUserRole(user_id, new_role)