环回 API 包括未按预期工作的过滤器 [英] Loopback API include filters not working as expected

查看:42
本文介绍了环回 API 包括未按预期工作的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了不同的模型来简化我想要实现的目标并消除混乱,尽管理论应该与我的实际项目相同.

I have used different models to simplify what I am trying to achieve and to remove clutter, although the theory should be the same as my actual project.

使用以下假设:一个系统只能有一种扬声器和一种放大器.

Use the following assumptions: A system can only have one type of speaker and one type of amp.

假设我有以下模型:

*** System ***
- id
- name
- speakerId
- ampId

*** Speaker ***
- id
- name

*** Amp ***
- id
- name

我已将以下内容添加到我的 System.json 模型文件中(我认为这是正确的):

I have added the following to my System.json model file (which i think is correct):

"relations": {
    "speakers": {
      "type": "hasOne",
      "model": "Speaker",
      "foreignKey": "speakerId"
    },
    "amps": {
      "type": "hasOne",
      "model": "Amp",
      "foreignKey": "ampId"
    }
  },

当我启动我的应用并打开 API Explorer 并创建一个新的 Speaker 或 Amp 实例时,它需要以下内容:

When I start my app and open the API Explorer and go to create a new instance of Speaker or Amp it expects the following:

  *** Speaker ***
- id
- name
- speakerId   *** Why is this here??? ***

如果我将 System.json 模型文件更改为如下所示(我认为这是错误的做法):

If I change the System.json model file to look like this (which I think is the wrong way to do it):

"relations": {
    "speakers": {
      "type": "hasOne",
      "model": "Speaker",
      "foreignKey": "id"
    },
    "amps": {
      "type": "hasOne",
      "model": "Amp",
      "foreignKey": "id"
    }
  },

在 API 资源管理器中一切正常.所以我添加了一些扬声器和放大器,然后添加了一个系统.

Everything looks right in the API explorer. So I add a few speakers and amps, then add a system.

当我向 System GET 请求添加包含过滤器时:

When I add an include filter to the System GET request:

{"include":["speakers","amps"]}

它包括扬声器和放大器,但使用 System.id 作为扬声器和放大器模型的索引,而不是 System.speakerId 和 System.ampId.所以要详细说明:

It includes the speakers and amps but using the System.id as the index for the Speakers and Amps Models instead of System.speakerId and System.ampId. So to ellaborate if the:

System.id = 5 
System.speakerId = 1
System.ampId = 3

它将包含 id 为 5 的扬声器和 id 为 5 的放大器,而不是包含 id 为 1 的扬声器和 id 为 3 的放大器.

It will include the speaker with an id of 5 and the amp with the id of 5 instead of including the speaker with an id of 1 and the Amp with an id of 3.

我希望能够列出所有系统,包括它们相关的扬声器和放大器.如何使用 Loopback 做到这一点.目前,我正在将上述模型与内存数据库一起使用,而不是我通常使用的模型来进行测试.任何帮助将不胜感激,因为我现在正要撕掉我的头发!

I want to be able to list all Systems including their related Speakers and Amps. How can this be done with Loopback. At the minute I am using the above models with in memory DB instead of my usual ones just to test. Any help would be appreciated as I am now at the point of tearing my hair out!

问候,詹姆斯

推荐答案

您的模型之间的关系不正确.您需要告诉 loopbackjs 使用 hasMany through relation 来获取您的数据.

Your relation between models is not correct. You need to tell loopbackjs to use hasMany through relation to get your data.

你的 system.json 应该改为

Your system.json should be changed to

"relations": {
"speaker": {
  "type": "belongsTo",
  "model": "Speaker",
  "foreignKey": "speakerId"
},
"amp": {
  "type": "belongsTo",
  "model": "Amp",
  "foreignKey": "ampId"
}},

你的扬声器应该是

"relations": {
    "amps": {
      "type": "hasMany",
      "model": "Amp",
      "foreignKey": "speakerId",
      "through": "System"
    },

amp.json

"relations": {
    "speakers": {
      "type": "hasMany",
      "model": "Speaker",
      "foreignKey": "ampId",
      "through": "System"
    },

这篇关于环回 API 包括未按预期工作的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆