问题描述
在 Strapi 中,每个用户定义的集合类型都有一个默认服务,允许创建/查找/更新/等.在相应的模型上.例如,Strapi 控制器中的以下代码将使用给定数据更新账单收集类型:
In Strapi every user-defined collection type has a default services which allow to create/find/update/etc. on the corresponding Model.For Example the below code in a Strapi controller will update a bill collection type with the given data:
await strapi.services.bill.update({id}, {verified: true, receipt_number})
但是对于User 内置集合类型没有服务.我需要通过自定义控制器更改用户的角色.
But there is no service for User built-in collection type.I need to change the user's role by a custom controller.
推荐答案
好的.我找到了解决方案.我们可以修改或创建自定义服务,并从这里的核心服务实现中获得启发:https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services
OK. I found the solution. We could modify or create a custom services and get inspired by this core services implementation here:https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services
就我而言,我已将此方法添加到自定义服务中:
for my case I have added this method to a custom service:
updateUserRole: async (user_id, role_id) => {
return await strapi.query('user', 'users-permissions').update({id: user_id}, {role: role_id});}
现在我可以像这样访问这项服务:
now I could access this service like this:
await strapi.services.customServiceName.updateUserRole(user_id, new_role)
这篇关于如何在 Strapi 中更新用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!