Skip to content

Instantly share code, notes, and snippets.

@distracteddev
Created April 15, 2012 04:33
Show Gist options
  • Save distracteddev/2390045 to your computer and use it in GitHub Desktop.
Save distracteddev/2390045 to your computer and use it in GitHub Desktop.
A Resourceful.js Question

The response from the couchDB view (This is the correct response):

{
	rows: [
		{
			key: "Test",
			value: 2
		}
	]
}
   // Incase it helps, what the MapReduce function is doing in this case is
   // returning an array of distinct tags as the keys, and their frequency as values. This is a standard view and is ripped 
   //right out of the couchDB guide. 

couchdb/index.js Line 140 - In Couchdb.prototype.view() - Here, the res object is correctly populated with the rows object from the couchDB resonse. However, it strips out only the doc/value attributes from the rows. This suggests that this function was only designed to return information on the values, and never the keys. This is what confuses me.

The r.value the above function returns is 2, which is then passed into the callback as an array defined on line 110/111 of resource.js. The result var that is passed to this callback is passed as an array (see below). Thus, the code block at line 126 is executed and the result var is passed into the map function at line 128 that subsequently seems to instantiate resourceful objects out of the result.

There are two problems here.

  1. The ._request function is ever only given the key's of a couchDB view back. (The key was stripped out in the function listed above).
  2. CouchDB views can return more than just filtered lists of document objects. Why are we automatically trying to instantiate the value/doc attributes of the returned response array - rows (see the above response).

The array that is passed into the callback defined within Resource._request Line 110.

     result: [2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment