Loopback hasManyThrough用户关系

时间:2017-06-23 17:12:44

标签: loopbackjs strongloop

我有这样的模特:

{
  "name": "Team",
  "plural": "teams",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "mixins": {
    "ModelRest": {}
  },
  "hidden": [
    "deleted"
  ],
  "filtered": [
    "userId",
    "archived"
  ],
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "createdAt": {
      "type": "date"
    },
    "deleted": {
      "type": "boolean"
    }
  },
  "validations": [],
  "relations": {
    "projects": {
      "type": "hasMany",
      "model": "Project"
    },
    "user": {
      "type": "belongsTo",
      "model": "user"
    },
    "users": {
      "type": "hasMany",
      "model": "User",
      "foreignKey": "",
      "through": "TeamMember"
    }
  },
  "acls": [],
  "methods": {}
}

TeamMember

{
  "name": "TeamMember",
  "plural": "team-members",
  "base": "Model",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "user"
    },
    "team": {
      "type": "belongsTo",
      "model": "Team"
    }
  },
  "acls": [],
  "methods": {}
}

用户

{
  "name": "user",
  "plural": "users",
  "base": "User",
  "idInjection": true,
  "mixins": {
    "ModelRest": {}
  },
  "hidden": [
    "realm",
    "emailVerified",
    "lastIP",
    "deleted",
    "utmSource",
    "utmMedium",
    "utmCampaign"
  ],
  "readOnly": [
    "statusId",
    "lastListId",
    "teamId",
    "subscriptionStart",
    "subscriptionExpiration",
    "apiKey"
  ],
  "properties": {
    "name": {
      "type": "string",
      "required": true,
      "mysql": {
        "columnName": "username"
      }
    },
    "password": {
      "type": "string",
      "required": true,
      "min": 5
    },
    "email": {
      "type": "string",
      "required": true
    },
    "createdAt": {
      "type": "date"
    },
    "updatedAt": {
      "type": "date"
    },
    "subscriptionStart": {
      "type": "date"
    },
    "subscriptionExpiration": {
      "type": "date"
    },
    "teamId": {
      "type": "number"
    },
    "sharePlan": {
      "type": "boolean"
    },
    "shareLeads": {
      "type": "boolean"
    },
    "timezone": {
      "type": "string"
    },
    "utmSource": {
      "type": "string"
    },
    "utmMedium": {
      "type": "string"
    },
    "utmCampaign": {
      "type": "string"
    },
    "lastIP": {
      "type": "string"
    },
    "deleted": {
      "type": "boolean"
    }
  },
  "validations": [],
  "relations": {
    "team": {
      "type": "belongsTo",
      "model": "Team"
    },
    "plan": {
      "type": "belongsTo",
      "model": "Plan"
    },
    "billingCycle": {
      "type": "belongsTo",
      "model": "BillingCycle"
    },
    "card": {
      "type": "belongsTo",
      "model": "Card"
    },
    "lastList": {
      "type": "belongsTo",
      "model": "List"
    },
    "status": {
      "type": "belongsTo",
      "model": "Status"
    },
    "accessTokens": {
      "type": "hasMany",
      "model": "AccessToken"
    }
  },
  "acls": [],
  "methods": {}
}

我在团队模型中创建了关系:

"users": {
  "type": "hasMany",
  "model": "User",
  "foreignKey": "",
  "through": "TeamMember"
}

但团队中的用户关系根本不起作用。在API资源管理器中,我看到了

GET / teams / {id} / user

没有

GET / teams / {id} / user s

为什么会这样? 我甚至用Loopback关系生成器创建了这种关系。结果相同。无法弄清楚错误在哪里。 Loopback doest看到这种关系。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这就是全部,因为在model-config.json中我有

  "TeamMember": {
    "dataSource": null,
    "public": true
  },

而不是:

  "TeamMember": {
    "dataSource": "db",
    "public": true
  },