MK, Luckily for you, some of your assumptions are wrong.
You should have no problem doing an "async-for-loop" with Node. Read this: http://metaduck.com/post/2675027550/asynchronous-iteration-patterns-in-node-js Also, you might not need a for-loop, as Couch lets you fetch several documents at the same time: http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Fetch_Multiple_Documents_With_a_Single_Request Cheers, a. On 22 May 2011 20:43, MK <[email protected]> wrote: > I'm pseudo cross posting this to the nodejs list, but the replies will > not cross-post so hopefully no one is too troubled by that. > > I'm new to both and I've hit a bit of a conundrum. > > I have a node request handler that presents a view of a couchDB > document. The document contains an array of other document id's. In > the view, I want to list those documents, and include a piece of > information from them (call it the "short description"). This means, > in addition to querying couch for the primary doc, I have to query it > for the short description of all the docs listed in the primary doc's > array of related pages. > > Sounds simple. I have the array, now all I have to do is > > var shortDescs = new Array(); > for (i in array) { > //query couch for data > shortDescs.push(data) > } > > Except the only way to query couch from node is via asynchronous http > requests. Ie, when this loop completes, it is very unlikely that the > shortDescs array will be properly populated. So this is a dead end. > > That's the way nodejs works, and it has that limitation. The way couch > works also results in some limitations. You cannot make a single > request for an arbitrary set of documents. You can set up a "view" > function that will return data from all the documents that match a > specific criteria *but that criteria must be hardcoded*. In other > words, you cannot create a view function that takes arbitrary > parameters, such as an array of ids. > > In short, AFAICT, it is simply impossible to do this using couchDB and > nodejs together, which seems a shame. > > I can see three choices to work around this: > > - Make the primary document's array an array of objects which contains > the short desc from the other docs. Obviously, this will result in a > tremendous amount of reduplication and wasted space if every doc has an > array of related docs that must contain information from those other > docs because I cannot query couch for an array of arbitrary docs by id. > > - Do a whole bunch of ajax calls when the page loads to complete the > information for each doc in the array, because I cannot use node to > assemble information from an arbitrary array of http requests to > couchDB. Obviously, this will result in increased server load. > > - call a couch view to return all short descriptions + > ids indiscriminately, and then choose the ones I want. Probably the > best choice, but IMO still a very poor compromise. > > Is there any other solution anyone sees here? > > -- > "Enthusiasm is not the enemy of the intellect." (said of Irving Howe) > "The angel of history[...]is turned toward the past." (Walter Benjamin) > >
