You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Jean-Felix Girard <je...@icloud.com> on 2013/12/31 14:47:46 UTC

erlang view: undefined value for key

Hi,

I recently had a problem dealing with undefined  values in my erlang views.   If I emit the value undefined, it sometimes breaks the view.

I did some tests:

With a very simple view (it emit the "bar" property):

{
   "_id": "_design/erl",
   "_rev": "2-79444cd647b4fc4728abcddf37df8da2",
   "views": {
       "foo": {
           "map": "fun({Doc}) -> Emit(proplists:get_value(<<\"bar\">>, Doc), null) end."
       }
   },
   "language": "erlang"
}

No doc: OK
$ curl localhost:5984/test/_design/erl/_view/foo
{"total_rows":0,"offset":0,"rows":[

]}

1 doc without the bar property: OK but with the string "undefined"

curl localhost:5984/test/_design/erl/_view/foo
{"total_rows":1,"offset":0,"rows":[
{"id":"dc06ad4d722586f0eb16db8772001e33","key":"undefined","value":null}
]}

Add one more doc with bar property "hello": still OK

curl localhost:5984/test/_design/erl/_view/foo
{"total_rows":2,"offset":0,"rows":[
{"id":"dc06ad4d722586f0eb16db8772001e33","key":"undefined","value":null},
{"id":"dc06ad4d722586f0eb16db877200277e","key":"hello","value":null}
]}

Change hello to  to null  (or false or true): Crash

curl localhost:5984/test/_design/erl/_view/foo
curl: (52) Empty reply from server

with couchdb console:
initial call: mochiweb_acceptor:init/3
    pid: <0.112.0>
    registered_name: []
    exception error: bad argument
      in function  list_to_binary/1
         called as list_to_binary([{couch_ejson_compare,atom_sort,
                                       [undefined],
                                       [{file,"couch_ejson_compare.erl"},
                                        {line,82}]},
                                   {couch_ejson_compare,less_erl,2,
                                       [{file,"couch_ejson_compare.erl"},
                                        {line,62}]},
                                   {couch_ejson_compare,less_json_ids,2,
                                       [{file,"couch_ejson_compare.erl"},
                                        {line,45}]},
                                   {lists,sort,2,
                                       [{file,"lists.erl"},{line,954}]},
                                   {couch_btree,query_modify,4,
                                       [{file,"couch_btree.erl"},{line,195}]},
                                   {couch_btree,add_remove,3,
                                       [{file,"couch_btree.erl"},{line,174}]},
                                   {couch_mrview_updater,
                                       '-write_kvs/4-fun-0-',4,
                                       [{file,"src/couch_mrview_updater.erl"},
                                        {line,230}]},
                                   {lists,zipwith,3,
                                       [{file,"lists.erl"},{line,436}]}])
      in call from couch_httpd:error_info/1 (couch_httpd.erl, line 808)
      in call from couch_httpd:send_error/2 (couch_httpd.erl, line 913)
      in call from couch_httpd:handle_request_int/5 (couch_httpd.erl, line 365)
      in call from mochiweb_http:headers/5 (mochiweb_http.erl, line 94)


But if I change back null to "hello" or a number, let say 99:  OK

$ curl localhost:5984/test/_design/erl/_view/foo
{"total_rows":2,"offset":0,"rows":[
{"id":"dc06ad4d722586f0eb16db8772001e33","key":"undefined","value":null},
{"id":"dc06ad4d722586f0eb16db877200277e","key":99,"value":null}
]}


Using a javascript view:

function(doc) {
emit(doc.bar, null);
}

Undefined values are automatically transformed to null...  And therefore, there is no crash.

Is this a bug ?  If undefined is not a valid value, should it refused it all the time ?  Or convert it automatically to null ?  
 

I worked around it by using Emit(proplists:get_value(<<\"bar\">>, Doc, null), null)  (with the null as third parameter) to avoid having undefined values.  

Thanks and happy new year!

Jeff