You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Hank Knight <hk...@gmail.com> on 2014/01/20 17:29:54 UTC

CouchDB: Using XMLHttpRequest in Show Functions

I discovered that new functions can be made available to all show and
list functions by modifying:
/share/couchdb/server/main.js

I have attached an example.  To test it, replace your version of
"main.js" with the one I attached.

Then create a design document like the one I attached.

Test it like this:
http://localhost/db/_design/showtell/_show/test

This causes the custom function in main.js to be executed and it
returns a result.

I want to use this method to retrieve data, like this:

function getPage(url) {
 var req = new XMLHttpRequest();
 req.open("GET", url, false);
 req.send(null);
 return(req.responseText);
}

Everything works until I use XMLHttpRequest.  Then I get an error:
XMLHttpRequest is not defined

How can I gain access to XMLHttpRequest from main.js?

Re: CouchDB: Using XMLHttpRequest in Show Functions

Posted by Dave Cottlehuber <dc...@jsonified.com>.
> Mon, Jan 20, 2014 at 12:29 PM, Hank Knight <hk...@gmail.com> 
> wrote: 
> I discovered that new functions can be made 
> available to all show and 
> list functions by modifying: 
> 
> /share/couchdb/server/main.js 
> 
> I have 
> attached an example. To test it, replace your version of 
> 
> "main.js" with the one I attached. 
> 
> Then 
> create a design document like the one I attached. 
> 
> 
> Test it like this: 
> http://localhost/db/_design/showtell/_show/test 
> 
> 
> This causes the custom function in main.js to be executed and 
> it 
> returns a result. 
> 
> I want 
> to use this method to retrieve data, like this: 
> 
> 
> function getPage(url) { 
> var req = new XMLHttpRequest(); 
> 
> req.open("GET", url, false); 
> req.send(null); 
> 
> return(req.responseText); 
> } 
> 
> 
> Everything works until I use XMLHttpRequest. Then I get an error: 
> 
> XMLHttpRequest is not defined 
> 
> How can 
> I gain access to XMLHttpRequest from main.js? 

Hi Hank,

There’s no support for running functions that have access to the outside world inside couchjs, by design. This is both for security reasons (don’t want a random doc getting access to the whole couchdb user’s filesystem), and also for sanity (result of a map on a doc should be idempotent).

I suspect what you probably want is to use an external handler possibly running under nodejs, functionally it will look the same but without the constraints you have:

https://couchdb.readthedocs.org/en/latest/config/externals.html and Paul’s excellent introduction https://couchdb.readthedocs.org/en/latest/externals.html note that the latter was written in 2010 and the functionality has been included in couch natively since then.

-- 
Dave Cottlehuber
Sent from my PDP11

Re: CouchDB: Using XMLHttpRequest in Show Functions

Posted by Hank Knight <hk...@gmail.com>.
Hello, is there any way to use XMLHttpRequest with CouchDB?


On Mon, Jan 20, 2014 at 12:29 PM, Hank Knight <hk...@gmail.com> wrote:
> I discovered that new functions can be made available to all show and
> list functions by modifying:
> /share/couchdb/server/main.js
>
> I have attached an example.  To test it, replace your version of
> "main.js" with the one I attached.
>
> Then create a design document like the one I attached.
>
> Test it like this:
> http://localhost/db/_design/showtell/_show/test
>
> This causes the custom function in main.js to be executed and it
> returns a result.
>
> I want to use this method to retrieve data, like this:
>
> function getPage(url) {
>  var req = new XMLHttpRequest();
>  req.open("GET", url, false);
>  req.send(null);
>  return(req.responseText);
> }
>
> Everything works until I use XMLHttpRequest.  Then I get an error:
> XMLHttpRequest is not defined
>
> How can I gain access to XMLHttpRequest from main.js?