incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Carey" <paul.p.ca...@gmail.com>
Subject Re: newbie help on issue of passing arguments to a view
Date Thu, 16 Oct 2008 17:18:18 GMT
The syntax of my original answer was careless. Key, startkey and
endkey do indeed need to be JSON encoded, and the whole url needs to
be URL encoded.

So using curl, you'd need to query with something like

'http://localhost:5984/scratch/_view/Datum/all_sorted_by_folk_cde_and_lat_and_lng?startkey=%5B%22a%22%5D&endkey=%5B%22a%22%2C%7B%7D%2C%7B%7D%5D&reduce=false'

I may also have misunderstood your original question. If you intend to
query only on folkcde and not lat / lng, you could simply
emit(doc.folkCde, doc);

You probably received the  "Cannot encode 'undefined' errors because
not all your docs contain a folkcde, lat or lng. In which case you
should do something like
if (doc.folkCde && doc.lat && doc.lng ) {
 emit([ ....
}

I put a short example on friendpaste
http://friendpaste.com/qEWErVGv

Paul

On Thu, Oct 16, 2008 at 5:23 PM, Douglas Fils <fils@iastate.edu> wrote:
> Paul,
>  Thanks..    I put into my views field
> {
>  "vartest": {
>      "map": "function (doc) { emit([doc.FolkCde, doc.Latitude,
> doc.Longitude], doc._id )}"
>  }
> }
>
> but if I try either of:
> 5984/lith/_view/test2/vartest?startkey=["sM"]&endkey=["sM",{},{}]
> 5984/lith/_view/test2/vartest?startkey=[sM]&endkey=[sM,{},{}]
> I get errors (URL and errors at end of email)
>
> I tried the quotes in the first one since the reference Bahadri, sent me to
> noted that the values to startkey endkey and key need to be JSON encoded.
>
> I also tried just 5984/lith/_view/test2/vartest?key="sM"  (and =sM)
>
> I am running version 0.8.0 if that make any difference in this. Have I just
> gotten a syntax error some place?
>
> Thanks
> Doug
>
>
> Using:  5984/lith/_view/test2/vartest?startkey=["sM"]&endkey=["sM",{},{}]
> [Thu, 16 Oct 2008 17:40:10 GMT] [info] [<0.26680.1>] Spawning new javascript
> instance.
> [Thu, 16 Oct 2008 17:40:10 GMT] [info] [<0.26670.1>] HTTP Error (code 500):
> {{nocatch,{bad_value,
>                                    "Cannot encode 'undefined' value as
> JSON"}},
>                       [{couch_query_servers,prompt,2},
>                        {couch_query_servers,'-map_docs/2-fun-0-',2},
>                        {lists,map,2},
>                        {lists,map,2},
>                        {couch_query_servers,map_docs,2},
>                        {couch_view,view_compute,2},
>                        {couch_view,update_group,1},
>                        {couch_view,update_loop,5}]}
>
> [Thu, 16 Oct 2008 17:40:10 GMT] [error] [emulator] Error in process
> <0.26680.1> with exit value: {{nocatch,{bad_value,"Cannot encode 'undefined'
> value as
> JSON"}},[{couch_query_servers,prompt,2},{couch_query_servers,'-map_docs/2-fun-0-',2},{lists,map,2},{lists,map,2},{couch_query_servers,map_docs,2},{couch_view,view_compute...
> [Thu, 16 Oct 2008 17:40:10 GMT] [info] [<0.26670.1>] 129.186.121.156 - -
> "GET /lith/_view/test2/vartest" 500
>
>
> Using:  5984/lith/_view/test2/vartest?startkey=[sM]&endkey=[sM,{},{}]
> [Thu, 16 Oct 2008 17:41:11 GMT] [info] [<0.26676.1>] HTTP Error (code 500):
> {'EXIT',{function_clause,
>                                  [{cjson,
>                                       tokenize,
>                                       ["sM]",{decoder,utf8,null,1,2,any}]},
>                                   {cjson,decode_array,3},
>                                   {cjson,json_decode,2},
>                                   {couch_httpd,
>                                       '-parse_view_query/1-fun-0-',
>                                       2},
>                                   {lists,foldl,3},
>                                   {couch_httpd,handle_db_request,3},
>                                   {couch_httpd,handle_request,2},
>                                   {mochiweb_http,headers,4}]}}
> [Thu, 16 Oct 2008 17:41:11 GMT] [info] [<0.26676.1>] 129.186.121.156 - -
> "GET /lith/_view/test2/vartest" 500
>
>
> Using: 5984/lith/_view/test2/vartest?key="sM"
> [Thu, 16 Oct 2008 17:42:29 GMT] [info] [<0.26690.1>] Spawning new javascript
> instance.
> [Thu, 16 Oct 2008 17:42:29 GMT] [info] [<0.26682.1>] HTTP Error (code 500):
> {{nocatch,{bad_value,
>                                    "Cannot encode 'undefined' value as
> JSON"}},
>                       [{couch_query_servers,prompt,2},
>                        {couch_query_servers,'-map_docs/2-fun-0-',2},
>                        {lists,map,2},
>                        {lists,map,2},
>                        {couch_query_servers,map_docs,2},
>                        {couch_view,view_compute,2},
>                        {couch_view,update_group,1},
>                        {couch_view,update_loop,5}]}
>
> [Thu, 16 Oct 2008 17:42:29 GMT] [info] [<0.26682.1>] 129.186.121.156 - -
> "GET /lith/_view/test2/vartest" 500
> [Thu, 16 Oct 2008 17:42:29 GMT] [error] [emulator] Error in process
> <0.26690.1> with exit value: {{nocatch,{bad_value,"Cannot encode 'undefined'
> value as
> JSON"}},[{couch_query_servers,prompt,2},{couch_query_servers,'-map_docs/2-fun-0-',2},{lists,map,2},{lists,map,2},{couch_query_servers,map_docs,2},{couch_view,view_compute...
>
>
>
> Paul Carey wrote:
>>>
>>>  I have set up a test server and I am able to store and call simple views
>>> like:
>>> function(doc) {   if (doc.FolkCde == 'S')  emit(doc.Latitude,
>>> doc.Longitude)
>>> }
>>>
>>> However,  I am curious if it's possible for couchdb to be sent arguments
>>> to
>>> a view?  For example could I do something like:
>>> ...:5984/lith/_view/test/v2/Mn  where the "Mn" argument is passed in and
>>> used for the doc.FolkCdr comparison?
>>>
>>
>> Not with the view you've defined, but you could try something like
>> function (doc) {
>>  emit( [doc.FolkCde, doc.lat, doc.lng], doc._id );
>> }
>>
>> and then query it with
>> _view/test/v2?startkey=[$folkCde]&endkey=[$folkCde,{},{}]
>>
>> where the {} in the endkey sort very late
>>
>> The emit above emits the doc id rather than the doc itself as you said
>> you've got very large quantities of data. Emitting the doc could make
>> the view very large.
>>
>>
>>>
>>> couchdb:   semi-dynamic and semi-structured data, static queries
>>> RDMS:  semi-static (and well structured) data, dynamic queries
>>>
>>
>> Sounds reasonable, I've heard others describe it this way too.
>>
>> Paul
>>
>
>

Mime
View raw message