couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jean-Felix Girard <>
Subject erlang view: undefined value for key
Date Tue, 31 Dec 2013 13:47:46 GMT

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


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

curl localhost:5984/test/_design/erl/_view/foo

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

curl localhost:5984/test/_design/erl/_view/foo

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,
      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

Using a javascript view:

function(doc) {
emit(, 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!


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message