Fetching subdocuments with angular $http

纵饮孤独 提交于 2019-12-14 03:17:15

问题


Say you have nested collections a, b, and c, which follow the following map:

{"collection":"a",
 "children":[{"collection":"b",
              "name":"bee",
              "children"[{"collection":"c","name":"cee"}]}]}

And here is a1, fetched from a MongoDb database with $http:

{"title":"title a1",
 "id":"a1",
 "bee":[{"id":"b1"},{"id":"b2"}],
 "other_array":[{"foo":"bar"},{"foo":"baz"}]}

Right now, in the bee array, we have only references (id). What we want is to keep following the map to update a1, and replace references by actual data.

It would entail fetching b1 and b2 data from database, which could each have cee arrays, whose elements we would need to fetch from the c collection.

I suppose one could easily create a dedicated backend function, that would take in a1, do all the fetching at once and return the end result;

but how would you get the fully detailed version of a1 by using multiple $http/$resource calls?

Should a recursive function be used?

Or would it be best to use $q and chained promises?

How to walk the map (to know which collections are relevant, and what their name is), retrieve the relevant b items, then the relevant c items, and at the very end update a1, so as to replace a1 with something like:

{"title":"title a1","id":"a1","bee":[{"id":"b1","title":"title b1","other_stuff":"blah blah","cee":[{"id":"c1","title":"title c1","c_specific":"hi there"}]},{"id":"b2","title":"title b2","other_stuff":null,"cee":[]}],"other_array":[{"foo":"bar"},{"foo":"baz"}]}

回答1:


As suggested in the comments by benjamin Gruenbaum,

making multiple http calls to fetch a single value you need will slow down your website's response time and provide a bad user experience

So I have dropped the idea altogether, and will just fetch one document from the database, which already contains all the nested arrays.

The only downside I see is that, when a subdocument is modified directly, all documents containing the subdocument will have to be manually modified.



来源:https://stackoverflow.com/questions/28381358/fetching-subdocuments-with-angular-http

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