You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Brian Candler <B....@pobox.com> on 2009/07/17 11:18:11 UTC
Why can't I use "var" in map code?
Could somebody explain this subtlety of Javascript to me?
This works fine:
$ curl -X POST -d '{"map":"x=123; function(doc) { emit(x,null); }"}' http://127.0.0.1:5984/mailme/_temp_view
{"total_rows":2,"offset":0,"rows":[
{"id":"54829ded88644559e6c81a7f482c5800","key":123,"value":null},
{"id":"ebf1585feb2109429a0cd62528e8ab4b","key":123,"value":null}
]}
But this doesn't:
$ curl -X POST -d '{"map":"var x=123; function(doc) { emit(x,null); }"}' http://127.0.0.1:5984/mailme/_temp_view
{"error":"compilation_error","reason":"expression does not eval to a function. (var x=123; function(doc) { emit(x,null); })"}
The only difference is the addition of the word 'var'.
(The reason: I'm writing a map function which needs to use a precalculated
lookup table)
Thanks,
Brian.
Re: Why can't I use "var" in map code?
Posted by Brian Candler <B....@pobox.com>.
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.
Re: Why can't I use "var" in map code?
Posted by Paul Davis <pa...@gmail.com>.
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;"}
On Fri, Jul 17, 2009 at 5:18 AM, Brian Candler<B....@pobox.com> wrote:
> Could somebody explain this subtlety of Javascript to me?
>
> This works fine:
>
> $ curl -X POST -d '{"map":"x=123; function(doc) { emit(x,null); }"}' http://127.0.0.1:5984/mailme/_temp_view
> {"total_rows":2,"offset":0,"rows":[
> {"id":"54829ded88644559e6c81a7f482c5800","key":123,"value":null},
> {"id":"ebf1585feb2109429a0cd62528e8ab4b","key":123,"value":null}
> ]}
>
> But this doesn't:
>
> $ curl -X POST -d '{"map":"var x=123; function(doc) { emit(x,null); }"}' http://127.0.0.1:5984/mailme/_temp_view
> {"error":"compilation_error","reason":"expression does not eval to a function. (var x=123; function(doc) { emit(x,null); })"}
>
> The only difference is the addition of the word 'var'.
>
> (The reason: I'm writing a map function which needs to use a precalculated
> lookup table)
>
> Thanks,
>
> Brian.
>