Wow, thanks a lot, it worked - woohoo! I wouldn't consider this as a bug, just something one should know. start() actually sounds like something that needs to be stopped somehow.
If you or somebody else could add me to the wiki contributors group, I would be happy to clean some stuff up there and add links to the current docs where appropriate (.../en/latest/). Should lead to less confusion. My wiki user name is BernhardGschwantner. Cheers, Bernhard 2013/9/18 Alexander Shorin <[email protected]> > Hi Bernhard, > > Sorry, I'd to check my code instead of post it blindly. And yes this > looks as bug for me. > > To fix this you have to return any value from list function like: > > function(head, req) { > start({"code": 301, "headers": {"Location": "http://google.com"}}); > return 'redirecting...'; > } > > this _any_ value may be also empty string, but not null. return ''; also > fixes. > > -- > ,,,^..^,,, > > > On Wed, Sep 18, 2013 at 5:04 PM, Bernhard Gschwantner > <[email protected]> wrote: > > Hi Alexander, > > > > Thanks a lot for the detailled answer! I completely understand what > you're > > saying. I've been following the JIRA issues COUCHDB-430, COUCHDB-514 etc. > > for years, since this redirect topic was one of the first obstacles I ran > > into when starting to use CouchDB. > > > > Nevertheless I still don't get a valid redirect, even with start(). It's > a > > bit embarrassing that I can't get a simple _list function right... ;-) > > > > > > I put this design doc into a new database "redirect": > > > > > > { > > "_id": "_design/redirect", > > "views": { > > "all": { > > "map": "function(doc) {\n\temit(doc._id, null);\n}" > > } > > }, > > "lists": { > > "redirect": "function(head, req) {\n\tstart({\n\t\t\"code\": > > 302,\n\t\t\"headers\": {\n\t\t\t\"Location\": \"http://www.google.com > > \"\n\t\t}\n\t});\n}" > > } > > } > > > > > > The couchdb output when calling > > http://localhost:5984/redirect/_design/test/_list/redirect/all > > > > {"error":"unknown_error","reason":"undef"} > > > > > > This is the couchdb.log output (level INFO): > > > > > > [error] [<0.20382.0>] Uncaught error in HTTP request: {error,undef} > > [info] [<0.20382.0>] Stacktrace: [{undefined,write_chunk,[[]],[]}, > > {couch_httpd,last_chunk,1, > > [{file, > > > > "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F% > > 2Fgit-wip-us.apache.org > > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"}, > > {line,692}]}, > > {couch_mrview_show,list_cb,2, > > [{file, > > > > "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F% > > 2Fgit-wip-us.apache.org > > > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview_show.erl"}, > > {line,254}]}, > > {couch_mrview,finish_fold,2, > > [{file, > > > > "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F% > > 2Fgit-wip-us.apache.org > > > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couch_mrview/src/couch_mrview.erl"}, > > {line,339}]}, > > {couch_query_servers,with_ddoc_proc,2, > > [{file, > > > > "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F% > > 2Fgit-wip-us.apache.org > > > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_query_servers.erl"}, > > {line,269}]}, > > {couch_httpd,etag_maybe,2, > > [{file, > > > > "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F% > > 2Fgit-wip-us.apache.org > > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"}, > > {line,600}]}, > > {couch_httpd_db,do_db_req,2, > > [{file, > > > > "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F% > > 2Fgit-wip-us.apache.org > > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd_db.erl"}, > > {line,234}]}, > > {couch_httpd,handle_request_int,5, > > [{file, > > > > "/Users/jan/Work/build-couchdb-mac/build-couchdb/git-build/https%3A%2F% > > 2Fgit-wip-us.apache.org > > %2Frepos%2Fasf%2Fcouchdb.git%3A1.4.0/src/couchdb/couch_httpd.erl"}, > > {line,332}]}] > > [info] [<0.20382.0>] 127.0.0.1 - - GET > > /redirect/_design/redirect/_list/redirect/all 500 > > [error] [<0.20382.0>] httpd 500 error response: > > {"error":"unknown_error","reason":"undef"} > > > > > > > > Is this a bug, or am I still missing something? > > > > Regards, > > Bernhard > > > > > > > > 2013/9/18 Alexander Shorin <[email protected]> > > > >> Hi Bernhard, > >> > >> you need to send redirect with start() function call. > >> > >> function(head, req) { > >> start({"code": 301, "headers": {"Location": "http://google.com"}}); > >> } > >> > >> http://docs.couchdb.org/en/1.4.x/query-servers.html > >> > >> Why so? start() function allows to send custom HTTP headers to the > >> client before any response (or with first chunk of it). When you > >> return or send() something before or without start() call, query > >> server implicitly called it with empty object payload which means that > >> CouchDB will send default HTTP status code and headers. Since CouchDB > >> sends response of list functions as chunked transfer, it doesn't > >> buffers all of it to let you change HTTP response status or headers > >> somewhere in the middle - what's why you need to define them in first > >> place and start() function explicitly points on this. > >> > >> For show functions your code will work since they produces only one > >> response object which contains response body and optionally may have > >> HTTP status and headers. With a little adaptation, the same is true > >> for update functions too. > >> > >> -- > >> ,,,^..^,,, > >> > >> > >> On Tue, Sep 17, 2013 at 5:34 PM, Bernhard Gschwantner > >> <[email protected]> wrote: > >> > Hello, > >> > > >> > I tried to redirect from a _list function without success. This code > >> throws > >> > a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3): > >> > > >> > function(head, req) { > >> > return { > >> > code : 301, > >> > headers : { > >> > Location : 'http://www.google.com' > >> > } > >> > }; > >> > } > >> > > >> > Is there something I am doing wrong or missing? > >> > > >> > The real-world use case is that we have several aliases to each > document > >> in > >> > the db, and all those aliases should be redirected to the real > document > >> id. > >> > We use a view to emit the aliases and a list function to redirect all > >> those > >> > aliases to the real url. > >> > > >> > We've been working around the previous issues with modifying headers > >> after > >> > getRow() by redirecting through JavaScript, but want to get rid of > this > >> > workaround eventually. > >> > > >> > Regards, > >> > Bernhard > >> > > >> > -- > >> > > >> > Bernhard Gschwantner > >> > Unser Wein G&U OG > >> > Kirchengasse 13/7, 1070 Wien > >> > > >> > mobil: +43 (6991) 971 32 96 > >> > tel: +43 (1) 971 32 95 > >> > e-mail: [email protected] > >> > twitter: @bernharduw <http://twitter.com/bernharduw> > >> > web: www.unserwein.at > >> > > > > > > > > -- > > > > Bernhard Gschwantner > > Unser Wein G&U OG > > Kirchengasse 13/7, 1070 Wien > > > > mobil: +43 (6991) 971 32 96 > > tel: +43 (1) 971 32 95 > > e-mail: [email protected] > > twitter: @bernharduw <http://twitter.com/bernharduw> > > web: www.unserwein.at > -- Bernhard Gschwantner Unser Wein G&U OG Kirchengasse 13/7, 1070 Wien mobil: +43 (6991) 971 32 96 tel: +43 (1) 971 32 95 e-mail: [email protected] twitter: @bernharduw <http://twitter.com/bernharduw> web: www.unserwein.at
