【问题标题】:Loopback Update model extend from User Model Error环回更新模型扩展自用户模型错误
【发布时间】:2018-08-10 09:26:37
【问题描述】:

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

"message": "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": {}
}

我在这里错过了什么吗?

【问题讨论】:

    标签: loopbackjs loopback


    【解决方案1】:

    由于您的实体是从 User 模型继承的,因此在使用时需要设置 'password' 和 'email' 属性POSTPUT 方法(因此创建/更新了 整个 对象) - 因为它们用于登录用户。

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

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

    【讨论】:

    • 嗨!谢谢!是的,我需要环回登录系统以及授权,所以我继承了用户模型。
    • 好吧,使用 PATCH 代替 PUT 对你有帮助吗?
    • 嗨,是的!
    猜你喜欢
    • 2018-08-10
    • 2015-09-21
    • 2020-11-16
    • 1970-01-01
    • 2017-07-31
    • 2016-10-12
    • 2011-02-28
    • 2013-12-06
    • 2018-01-21
    相关资源
    最近更新 更多