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

查看:188
本文介绍了环回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并转到创建议长的新实例或功放,预计如下:

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.

当我添加包括过滤器系统GET请求:

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

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

它包括扬声器和放大器,但使用System.id作为扬声器的指数和AMPS模式,而不是System.speakerId和System.ampId。因此,要ellaborate如果:

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

这将包括具有5一个id扬声器和放大器以5的ID代替包括为1的id和安培与3的ID的扬声器的

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.

我希望能够列出所有系统,包括其相关的扬声器和放大器。这可怎么用回环完成。在一分钟我使用上述模型,在内存数据库,而不是我平常的人只是为了测试。任何帮助将是AP preciated因为我现在在撕扯我的头发的地步!

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!

问候,
詹姆斯

Regards, James

推荐答案

您模型之间的关系是不正确的。你需要告诉loopbackjs使用的hasMany通过关系来获取数据。

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

amp.json

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

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

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