LoopBack:如何创建扩展的用户模型关系

LoopBack: How to Create an Extended User Model Relation

我无法从 User 模型的扩展模型创建关系。有关完整的详细信息 - 请参阅 the issue I opened。我从名为 userUser 扩展模型与名为 account.

的模型创建了 hasMany / belongsTo 关系

common/models/user.json

{
  "name": "user",
  "plural": "users",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "accounts": {
      "type": "hasMany",
      "model": "account",
      "foreignKey": "userId"
    }
  },
  "acls": [{
    "accessType": "EXECUTE",
    "principalType": "ROLE",
    "principalId": "$authenticated",
    "permission": "ALLOW",
    "property": "__create__accounts"
  }],
  "methods": {}
}

common/models/account.json

{
  "name": "account",
  "plural": "accounts",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": "userId"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW",
      "property": "create"
    }
  ],
  "methods": {}
}

问题

在 Loopback API 资源管理器中创建用户、登录并设置访问令牌后,我尝试通过 POST 请求创建用户帐户条目(/users/{id}/accounts 其中 {id}代表用户id)。响应是 404,并带有以下错误消息:

Shared class  \"User\" has no method handling POST /594fcbeee223ce23620a3e12/accounts?access_token=Dg8qkXIHEck9fS0taCPWxklSVwD6HivN7iVU0lq0SBhaXQ8dMJmo6j8WxQGBNKRD

我已按照文档进行操作并尝试了一些操作,即使根本没有设置 ACL - 我也看到了这个问题。我创建了一个 full example project here.

有没有人运行以前参与过这个?我猜我需要为我的 user 模型添加更多 ACL 设置,但我不清楚那会是什么。

您需要在 model-config.json

中隐藏基础 User 模型
{
  "User": {
    "dataSource": "db",
    "public": false
  },
  ...
}

由于它对远程处理可见,因此它仍在您的扩展模型上使用。有一个警告形式的小提示,可以指出这个问题。您会在应用程序启动时看到它。

警告:覆盖远程类型用户