环回无法通过关系获取数据

时间:2017-03-15 16:06:35

标签: node.js loopbackjs strongloop loopback

基于用户模型的会员模型

   {
      "name": "Member",
      "base": "User",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "nickname": {
          "type": "string"
        }
      },
      "validations": [],
      "relations": {
        "messages": {
          "type": "hasMany",
          "model": "Message",
          "foreignKey": ""
        },
        "followers": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followeeId",
          "through": "Follow"
        },
        "followings": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followerId",
          "through": "Follow"
        }
      },
      "acls": [
        {
          "accessType": "*",
          "principalType": "ROLE",
          "principalId": "$everyone",
          "permission": "DENY"
        },
        {
          "principalType": "ROLE",
          "principalId": "$owner",
          "permission": "ALLOW",
          "property": "__get__followers"
        },
        {
          "principalType": "ROLE",
          "principalId": "$owner",
          "permission": "ALLOW",
          "property": "__get__followings"
        },
        {
          "principalType": "ROLE",
          "principalId": "$owner",
          "permission": "ALLOW",
          "property": "__get__messages"
        }
      ],
      "methods": {}
    }

关注模特

{
  "name": "Follow",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "follower": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": ""
    },
    "followee": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": {}
}

示例1

enter image description here

当我在数据库中使用此数据时,当我尝试使用成员1获取关注者和关注者时,我会遇到相同的错误。

Erreur non traitée pour la demande GET /api/Members/1/followers?access_token=t0oAVZM2CLJ7XLqQm2zxz8wj3fLmtUATHopDM40WYknLURbiObpVAlSD3DBEIOfv : Error: La relation "member" n'est pas définie pour le modèle Follow
        at processIncludeItem (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/include.js:289:10)
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/include.js:180:5
        at /home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:3025:16
        at eachOfArrayLike (/home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:940:9)
        at eachOf (/home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:990:5)
        at Object.eachLimit (/home/thomas/Bureau/Projet/Project/node_modules/async/dist/async.js:3089:3)
        at Function.Inclusion.include (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/include.js:179:9)
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-connector-postgresql/node_modules/loopback-connector/lib/sql.js:1203:44
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:172:22
        at doNotify (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:99:49)
        at PostgreSQL.ObserverMixin._notifyBaseObservers (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:122:5)
        at PostgreSQL.ObserverMixin.notifyObserversOf (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:97:8)
        at cbForWork (/home/thomas/Bureau/Projet/Project/node_modules/loopback-datasource-juggler/lib/observer.js:162:14)
        at /home/thomas/Bureau/Projet/Project/node_modules/loopback-connector-postgresql/node_modules/loopback-connector/lib/sql.js:428:7
        at Query.<anonymous> (/home/thomas/Bureau/Projet/Project/node_modules/loopback-connector-postgresql/lib/postgresql.js:162:7)
        at Query.handleReadyForQuery (/home/thomas/Bureau/Projet/Project/node_modules/pg/lib/query.js:124:10)

示例2

enter image description here

当我尝试使用成员1获取关注者时,数据库中存在此数据。 enter image description here

我不明白为什么我无法在这两个例子中获取数据。顺便说一句,我正在使用PostgreSQL。感谢。

1 个答案:

答案 0 :(得分:1)

您需要根据Follow修复Member模型中的关系。

{
  "name": "Follow",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "follower": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": "followeeId"
    },
    "followee": {
      "type": "belongsTo",
      "model": "Member",
      "foreignKey": "followerId"
    }
  },
  "acls": [],
  "methods": {}
}

并在member.json

...
"followers": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followeeId",
          "keyThrough": "followerId",
          "through": "Follow"
        },
        "followings": {
          "type": "hasMany",
          "model": "Member",
          "foreignKey": "followerId",
          "keyThrough": "followeeId",
          "through": "Follow"
        }
...