incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Candler <B.Cand...@pobox.com>
Subject Re: Why can't I use "var" in map code?
Date Fri, 17 Jul 2009 15:54:49 GMT
On Fri, Jul 17, 2009 at 05:49:23AM -0400, Paul Davis wrote:
> Odd.
> 
> Try one of these:
> 
> {"map":"var x=123;\nfunction(doc) { emit(x,null); }"}
> {"map":"var x=123; function(doc) { emit(x,null); };"}
> {"map":"var x=123; var f = function(doc) { emit(x,null); }; f;"}

All those fail.

$ curl -X POST -d '{"map":"var x=123;\nfunction(doc) { emit(x,null); }"}' http://localhost:5984/mailme/_temp_view
{"error":"compilation_error","reason":"expression does not eval to a function. (var x=123;\nfunction(doc)
{ emit(x,null); })"}
$ curl -X POST -d '{"map":"var x=123; function(doc) { emit(x,null); };"}' http://localhost:5984/mailme/_temp_view
{"error":"compilation_error","reason":"expression does not eval to a function. (var x=123;
function(doc) { emit(x,null); };)"}
$ curl -X POST -d '{"map":"var x=123; var f = function(doc) { emit(x,null); }; f;"}' http://localhost:5984/mailme/_temp_view
{"error":"compilation_error","reason":"expression does not eval to a function. (var x=123;
var f = function(doc) { emit(x,null); }; f;)"}

It's not a biggie, but it was rather annoying that code which I had tested
and was working fine in the js shell, using load("filename.js"), didn't work
when I put it into a map. After some cutting and pasting it turned out that
'var' was the culprit.

Even this doesn't work at the top level in a map definition:

  bar = [...];
  for (var i in bar) { ... }
       ^^^^^

To avoid a global index variable 'i' I ended up doing this:

  bar = [...];
  (function() {
    for (var i in bar) ...
  })();

Regards,

Brian.

Mime
View raw message