如何在strapi.js中创建自定义用户

问题描述 投票:0回答:2

我正在开发一个 Strapi 应用程序,我有 3 种内容类型: - 用户(strapi自带的) - 轮廓 - 员工(有一个用户,有一个个人资料)

这是我的代码:

async create(data, { files } = {}) {
    const profileObj = data.profile
    const employeeObj = {
        salary_type: data.salarytype,
        salary: data.salary
    }
    const userObj = data.user
    profileObj.address = data.address

    const user = await strapi.query('user').create(userObj);
    const profile = await strapi.query('profile').create(profileObj);
    const employee = await strapi.query('employee').create(employeeObj);
    employee.user = user
    employee.profile = profile

    if (files) {
      // automatically uploads the files based on the entry and the model
      await strapi.entityService.uploadFiles(employee, files, {
        model: 'profile',
      });
      return this.findOne({ id: employee.id });
    }

    return employee;
  },

它正在工作,但我创建了另一个用户控制器/服务和模型,因为当我尝试不创建新用户 C/S/M 时,它给了我一个错误。

请问有什么建议吗?

api koa strapi
2个回答
1
投票

参考
创建用户查询应该是这样的。

const user = await strapi.query('user', 'users-permissions').create(userObj);

这样您就不需要创建新的用户 API 并使用 Strapi 附带的 API。


0
投票

试试这个:

const res = await strapi.entityService.create(
        'plugin::users-permissions.user',
        {
          data: {
            username: 'your-username',
            email: "email@",
            provider: 'local',
            blocked: false,
            password: 'raw-password',
            role: 3,
          },
        }
      );
© www.soinside.com 2019 - 2024. All rights reserved.