首页 > 解决方案 > 环回更新模型扩展自用户模型错误

问题描述

我创建了一个以 User 作为基础模型的 Artist 模型。我试图通过 Loopback 资源管理器更新艺术家模型,但出现错误:

“消息”:“ValidationError:Artist实例无效。详细信息:password 不能为空白(值:未定义);email不能为空白(值:未定义)。”

我已经注册了一个示例信息,并且我在 MongoDB 的艺术家数据库中有它。我不知道为什么我只是想更新现有数据时需要在电子邮件和密码字段中输入一些内容?

在此处输入图像描述

这是我的艺术家 JSON:

{
  "name": "Artist",
  "base": "User",
  "idInjection": false,
  "options": {
    "validateUpsert": true,
    "strictObjectIDCoercion": true
  },
  "properties": {
    "firstName": {
      "type": "string"
    },
    "middleName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "telephoneNumber": {
      "type": "string"
    },
    "country": {
      "type": "string"
    },
    "city": {
      "type": "string"
    },
    "genre": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "pricing": {
      "type": "string"
    },
    "profilePicture": {
      "type": "string"
    }
  },
  "validations": [],
  "acls": [
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW",
      "property": "create"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": [
        "updateAttributes",
        "deleteById"
      ]
    },
    {
      "accessType": "READ",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],

  "methods": {}
}

我在这里错过了什么吗?

标签: loopbackjsloopback

解决方案


因为您的实体是从User模型继承的,所以在使用POSTPUT方法时需要设置“密码”和“电子邮件”属性(因此创建/更新整个对象) - 因为它们用于登录用户.

如果您只是想在不传递整个数据的情况下更新您的艺术家,请使用PATCH。(然后'密码'和'电子邮件'将不再是强制性的)。

如果您不需要使用 Loopback 的登录系统,请不要从User模型继承您的实体。(或者你还有其他理由这样做吗?)


推荐阅读