【问题标题】:Loopback: Relation Through - not working环回:关系通过 - 不工作
【发布时间】:2018-05-02 07:10:28
【问题描述】:

所以,我被困在一个问题上,这应该很简单,我确信我遗漏了一些明显的东西

我正在关注此文档:

所以我有 3 张桌子

客户,团队,客户团队

client.json

{
  "name": "client",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "teams": {
      "type": "hasMany",
      "model": "team",
      "foreignKey": "teamId",
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

team.json

{
  "name": "team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "type": {
      "type": "string",
      "required": true,
      "default": "first-team"
    },
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "clients": {
      "type": "hasMany",
      "model": "client",
      "foreignKey": "clientId",
      "through": "client-team"
    }
  },
  "acls": [],
  "methods": {}
}

client-team.json

{
  "name": "client-team",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "clientId": {
      "type": "string",
      "required": true
    },
    "teamId": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "client": {
      "type": "belongsTo",
      "model": "Client",
      "foreignKey": "clientId"
    },
    "team": {
      "type": "belongsTo",
      "model": "Team",
      "foreignKey": "teamId"
    }
  },
  "acls": [],
  "methods": {}
}

所以所有关系都设置正确(我认为)......

那么在我的客户中,我确实有 1 个客户

[
  {
    "name": "Client name",
    "id": "59876185508eb519385779c6"
  }
]

在我的团队中,我有很多,但可以肯定的是:

[
  {
    "type": "type",
    "name": "Team name",
    "id": "5ae8a37add2989a32d37f83d"
  }
]

然后我去我的

localhost:3000/explorer

发布客户团队

喜欢这个

{
    "clientId": "59876185508eb519385779c6",
    "teamId": "5ae8a37add2989a32d37f83d"
}

我得到 200 响应:

{
  "clientId": "59876185508eb519385779c6",
  "teamId": "5ae8a37add2989a32d37f83d",
  "id": "5ae961873a7e3b33f0579fc3"
}

所以连接就在那里......

但是,当我转到“GET client/id”并执行时

ID:59876185508eb519385779c6 过滤器:{"include":["teams"]}

这是回复

{
  "name": "Chelsea FC",
  "id": "59876185508eb519385779c6",
  "teams": []
}

同样的情况发生在我使用的“GET teams/id”中

id: 5ae8a37add2989a32d37f83d 过滤器:{"include":["clients"]}

或者如果我转到“GET teams/{id}/clients” 并放 编号:5ae8a37add2989a32d37f83d

我明白了

[]

那我做错了什么?我确定我错过了一件愚蠢而明显的事情:/

如果有任何不同,请使用 mongo

【问题讨论】:

    标签: mongodb api foreign-keys relational-database loopback


    【解决方案1】:

    这里有三个问题:

    1. 您将 mongodb 标识符描述为字符串,这就是您在数据库中存储字符串而不是对象 ID 的原因。 (这不是必需的,因为数据源应该了解真实类型)
    2. 您的模型以小写字母开头。关系中也应该如此。 (问题的第一部分,它正在修复 ids 问题)
    3. 客户和团队模型的关系不正确(问题的第二部分,正在修复中)

    client-team.json

    {
      "name": "client-team",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "clientId": {
          "type": "objectId", // !!! changed (not required)
          "required": true
        },
        "teamId": {
          "type": "objectId", // !!! changed (not required)
          "required": true
        }
      },
      "validations": [],
      "relations": {
        "client": {
          "type": "belongsTo",
          "model": "client",  // !!! changed
          "foreignKey": "clientId"
        },
        "team": {
          "type": "belongsTo",
          "model": "team",  // !!! changed
          "foreignKey": "teamId"
        }
      },
      "acls": [],
      "methods": {}
    }
    

    client.json

    {
      "name": "client",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "name": {
          "type": "string",
          "required": true
        }
      },
      "validations": [],
      "relations": {
        "teams": {
          "type": "hasMany",
          "model": "team",
          "foreignKey": "clientId", // !!! changed (we describing id of this model, not team)
          "through": "client-team"
        }
      },
      "acls": [],
      "methods": {}
    }
    

    team.json

    {
      "name": "team",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "type": {
          "type": "string",
          "required": true,
          "default": "first-team"
        },
        "name": {
          "type": "string",
          "required": true
        }
      },
      "validations": [],
      "relations": {
        "clients": {
          "type": "hasMany",
          "model": "client",
          "foreignKey": "teamId", // !!! changed (the same as the previous)
          "through": "client-team"
        }
      },
      "acls": [],
      "methods": {}
    }
    

    【讨论】:

    • @DS_web_developer 让我检查一下
    • @DS_web_developer 重新检查我的评论
    • 不...仍然是 0,它不会改变任何事情...资源管理器仍然给我以字符串形式发布的选项...如果我这样做...仍然会得到空数组
    • 我刚刚将它重新创建为一个新项目......它按预期工作......所以我可能在其他地方有一些错误。感谢您的帮助
    • 嗯....没那么快...我用 db 模型而不是 mongo 重新创建了它,其中 id 是数字.....我尝试用 mongo 来做...不工作:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2017-08-28
    • 2016-05-08
    相关资源
    最近更新 更多