回环API包含筛选器不按预期工作
我使用了不同的模型来简化我想要实现的目标,并且消除混乱,虽然理论应该和我的实际项目一样。
使用以下假设:系统只能有一种扬声器和一种types的放大器。
说我有以下型号:
*** System *** - id - name - speakerId - ampId *** Speaker *** - id - name *** Amp *** - id - name
我已经添加了以下到我的System.json模型文件(我认为是正确的):
"relations": { "speakers": { "type": "hasOne", "model": "Speaker", "foreignKey": "speakerId" }, "amps": { "type": "hasOne", "model": "Amp", "foreignKey": "ampId" } },
当我启动我的应用程序并打开API资源pipe理器并创build扬声器或放大器的新实例时,它期望以下内容:
*** Speaker *** - id - name - speakerId *** Why is this here??? ***
如果我更改System.json模型文件看起来像这样(我认为是这样做的错误方法):
"relations": { "speakers": { "type": "hasOne", "model": "Speaker", "foreignKey": "id" }, "amps": { "type": "hasOne", "model": "Amp", "foreignKey": "id" } },
一切正常在API浏览器。 所以我添加几个扬声器和放大器,然后添加一个系统。
当我添加一个包含filter到系统GET请求:
{"include":["speakers","amps"]}
它包括扬声器和放大器,但使用System.id作为扬声器和放大器模型的索引,而不是System.speakerId和System.ampId。 所以要精心制作,如果:
System.id = 5 System.speakerId = 1 System.ampId = 3
它将包括标识为5的扬声器和标识为5的扬声器,而不包括标识为1的扬声器和标识为3的扬声器。
我希望能够列出所有系统,包括其相关的扬声器和放大器。 这怎么可以用Loopback完成。 在这一刻,我正在使用上述模型与内存数据库,而不是我通常的testing。 任何帮助,将不胜感激,因为我现在正在撕裂我的头发!
问候,詹姆斯
模型之间的关系是不正确的。 您需要告诉loopbackjs使用hasMany通过关系来获取您的数据。
你的system.json应该改成
"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" },