Multiple includes on different depths in a LoopBack/Node.js query

妖精的绣舞 提交于 2019-12-12 04:40:27

问题


I'm trying to load an object on a node.js Loopback server and I want to load multiple related objects in the same query. These related objects are on different depths. This is the query:

/api/MyUsers/114?filter[include][institutes]=institute&filter[include]=departments&filter[include]=profiles

Notice that [institutes]=institute have two levels and departments and profiles only one.

This returns me an error: 500 Internal server error. Error: Relation "0" is not defined for User model.

On node.js debug I noticed that the final JSON query generated seems to be wrong:

{ include: 
     { '0': 'departments',
       '1': 'profiles',
       institutes: 'institute' } },
     ...
}

Here is the model for MyUser:

{
  "name": "MyUser",
  "plural": "MyUsers",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "mixins": {
    "Versioning": true,
    "Tenant": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    },
    "email": {
      "type": "string"
    }
  }
  "validations": [],
  "relations": {
    "departments": {
      "type": "hasMany",
      "model": "UserDepartment",
      "foreignKey": "user_id"
    },
    "institutes": {
      "type": "hasMany",
      "model": "UserInstitute",
      "foreignKey": "user_id"
    },
    "profiles": {
      "type": "hasMany",
      "model": "UserProfile",
      "foreignKey": "user_id"
    },
  },
  "acls": [],
  "methods": {}
}

Has anyone else faced this problem? Is it a loopback bug? If so, how can I turn around for a solution?


回答1:


500 Internal server error. Error: Relation "0" is not defined for User model.

This error says that your User model does not have a relation which is called departaments. In your definition of the User model, make sure that you added a relation for the departaments model. (By the way, if departaments is an English word, it has a spelling problem, departments is correct.)

As this link says:

An include filter enables you to include results from related models in a query, for example, models that have belongsTo or hasMany relations, to optimize the number of requests.

This link can help you to define a relation for your model:

https://strongloop.com/strongblog/defining-and-mapping-data-relations-with-loopback-connected-models/

If you are using the built-in User model, you can extend it as below link says:

https://loopback.io/doc/en/lb3/Extending-built-in-models.html

Edit After Comment:

One more explanation about filters:

The meaning of filter[include][institutes]=institute filter is:

Include 'institutes' which is a relation of User and also include 'institute' which is a relation of 'institutes'. I tested this feature in one of my projects and it works fine.



来源:https://stackoverflow.com/questions/45760175/multiple-includes-on-different-depths-in-a-loopback-node-js-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!