Populating a Many to Many Through Relation in Sails.js

烂漫一生 提交于 2019-12-24 03:46:13

问题


Sails.js offers a way to populate objects in Many to Many relationship via its Blueprint API. Thus, GET /categorymodel/:categoryid/productscollection will return all products that belong to the specified category. Link.

For Many to Many Through relations, the docs advises to use multiple One to Many relations via an intermediary model to achieve the desired organization. The downside of this is there's no way to populate objects via the built-in Blueprint API. Link.

So, while querying for products belonging to a particular category, I query GET /categorymodel/:categoryid/intermediarycollection to get something like

[{categoryid: 1, productid: 1, id: 1}, {categoryid: 1, productid: 2, id: 2}, {categoryid: 1, productid: 3, id: 3}, ... n]

This means the client will have to GET /products/:productid n times.

How do I process the populate() result (and not edit/override it) to add the product object before sending to the client? Is there a better way of doing this?


回答1:


Yes! It is possible, but it is kind of an hack..?

The underlaying behaviour of the blueprints, is to call res.ok on your result from database. What if we changed the behavior of res.ok, and added a filter or similar custom needs?

Take a look on this answer for more details: https://stackoverflow.com/a/41932352/36975



来源:https://stackoverflow.com/questions/29254826/populating-a-many-to-many-through-relation-in-sails-js

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