Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

angularjs - Fetching subdocuments with angular $http

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"}]}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...