You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Anthony Kaufman <an...@gmail.com> on 2013/08/16 23:03:03 UTC

CoffeeScript CommonJS Module Function

I've been trying to define a CoffeeScript CommonJS Module Function without success. I can translate the same function into Javascript and it works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.

Here's the CoffeeScript document:
{
	_id: "_design/test",
	language: "coffeescript",
	lib: {
		say_hello: "exports.callback = (name) -> 'Hello ' + name"
	}
	test: {
		map: "(doc) ->
	say_hello = require('views/lib/say_hello').callback
	emit(say_hello(doc._id), doc)"
	}
}

When ran, it returns the following errors for each document:
function raised exception (new TypeError("say_hello is not a function", "undefined", 5)) with doc._id

Here's the Javascript document which works just fine:
{
	_id: "_design/test",
	language: "javascript",
	lib: {
		say_hello: "exports.callback = function(name) { return 'Hello ' + name }"
	}
	test: {
		map: "function(doc) { say_hello = require('views/lib/say_hello').callback; emit(say_hello(doc._id), doc) }"
	}
}

Currently, I have all of my views in a real document defined in CoffeeScript and I'd really rather not convert them all to Javascript. Any help would be appreciated.

- Anthony

Re: CoffeeScript CommonJS Module Function

Posted by Anthony Kaufman <an...@gmail.com>.
Great, thanks Jan. I'll take a look at util.js and see if I can make anything of it.

Thanks again,
Anthony

On Aug 20, 2013, at 1:51 PM, Jan Lehnardt <ja...@apache.org> wrote:

> 
> On Aug 20, 2013, at 22:49 , Anthony Kaufman <an...@gmail.com> wrote:
> 
>> I finally gave up and just compiled my CoffeeScripts to Javascript before I saved them into Couch. That way I can maintain the scripts in CoffeeScript (which I like), and  store them as Javascript (which CouchDB apparently likes). I had a script which was automating the save anyway so this wasn't too tricky (now I'm just bragging).
>> 
>> I still think that this could be a real live bug, or lack of a feature anyway. Where would be the best place to report something like that?
> 
> http://issues.apache.org/jira/browse/COUCHDB :)
> 
> Thanks!
> 
> I agree this is a bug and should be fixed. Maybe https://github.com/apache/couchdb/blob/master/share/server/util.js#L13 and following give you a clue why this might fail in CS?
> 
> Best
> Jan
> -- 
> 
>> 
>> Thanks, Anthony
>> 
>> On Aug 16, 2013, at 2:03 PM, Anthony Kaufman <an...@gmail.com> wrote:
>> 
>>> I've been trying to define a CoffeeScript CommonJS Module Function without success. I can translate the same function into Javascript and it works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
>>> 
>>> Here's the CoffeeScript document:
>>> {
>>> 	_id: "_design/test",
>>> 	language: "coffeescript",
>>> 	lib: {
>>> 		say_hello: "exports.callback = (name) -> 'Hello ' + name"
>>> 	}
>>> 	test: {
>>> 		map: "(doc) ->
>>> 	say_hello = require('views/lib/say_hello').callback
>>> 	emit(say_hello(doc._id), doc)"
>>> 	}
>>> }
>>> 
>>> When ran, it returns the following errors for each document:
>>> function raised exception (new TypeError("say_hello is not a function", "undefined", 5)) with doc._id
>>> 
>>> Here's the Javascript document which works just fine:
>>> {
>>> 	_id: "_design/test",
>>> 	language: "javascript",
>>> 	lib: {
>>> 		say_hello: "exports.callback = function(name) { return 'Hello ' + name }"
>>> 	}
>>> 	test: {
>>> 		map: "function(doc) { say_hello = require('views/lib/say_hello').callback; emit(say_hello(doc._id), doc) }"
>>> 	}
>>> }
>>> 
>>> Currently, I have all of my views in a real document defined in CoffeeScript and I'd really rather not convert them all to Javascript. Any help would be appreciated.
>>> 
>>> - Anthony
>> 
> 


Re: CoffeeScript CommonJS Module Function

Posted by Jan Lehnardt <ja...@apache.org>.
On Aug 20, 2013, at 22:49 , Anthony Kaufman <an...@gmail.com> wrote:

> I finally gave up and just compiled my CoffeeScripts to Javascript before I saved them into Couch. That way I can maintain the scripts in CoffeeScript (which I like), and  store them as Javascript (which CouchDB apparently likes). I had a script which was automating the save anyway so this wasn't too tricky (now I'm just bragging).
> 
> I still think that this could be a real live bug, or lack of a feature anyway. Where would be the best place to report something like that?

http://issues.apache.org/jira/browse/COUCHDB :)

Thanks!

I agree this is a bug and should be fixed. Maybe https://github.com/apache/couchdb/blob/master/share/server/util.js#L13 and following give you a clue why this might fail in CS?

Best
Jan
-- 

> 
> Thanks, Anthony
> 
> On Aug 16, 2013, at 2:03 PM, Anthony Kaufman <an...@gmail.com> wrote:
> 
>> I've been trying to define a CoffeeScript CommonJS Module Function without success. I can translate the same function into Javascript and it works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
>> 
>> Here's the CoffeeScript document:
>> {
>> 	_id: "_design/test",
>> 	language: "coffeescript",
>> 	lib: {
>> 		say_hello: "exports.callback = (name) -> 'Hello ' + name"
>> 	}
>> 	test: {
>> 		map: "(doc) ->
>> 	say_hello = require('views/lib/say_hello').callback
>> 	emit(say_hello(doc._id), doc)"
>> 	}
>> }
>> 
>> When ran, it returns the following errors for each document:
>> function raised exception (new TypeError("say_hello is not a function", "undefined", 5)) with doc._id
>> 
>> Here's the Javascript document which works just fine:
>> {
>> 	_id: "_design/test",
>> 	language: "javascript",
>> 	lib: {
>> 		say_hello: "exports.callback = function(name) { return 'Hello ' + name }"
>> 	}
>> 	test: {
>> 		map: "function(doc) { say_hello = require('views/lib/say_hello').callback; emit(say_hello(doc._id), doc) }"
>> 	}
>> }
>> 
>> Currently, I have all of my views in a real document defined in CoffeeScript and I'd really rather not convert them all to Javascript. Any help would be appreciated.
>> 
>> - Anthony
> 


Re: CoffeeScript CommonJS Module Function

Posted by Anthony Kaufman <an...@gmail.com>.
I finally gave up and just compiled my CoffeeScripts to Javascript before I saved them into Couch. That way I can maintain the scripts in CoffeeScript (which I like), and  store them as Javascript (which CouchDB apparently likes). I had a script which was automating the save anyway so this wasn't too tricky (now I'm just bragging).

I still think that this could be a real live bug, or lack of a feature anyway. Where would be the best place to report something like that?

Thanks, Anthony

On Aug 16, 2013, at 2:03 PM, Anthony Kaufman <an...@gmail.com> wrote:

> I've been trying to define a CoffeeScript CommonJS Module Function without success. I can translate the same function into Javascript and it works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
> 
> Here's the CoffeeScript document:
> {
> 	_id: "_design/test",
> 	language: "coffeescript",
> 	lib: {
> 		say_hello: "exports.callback = (name) -> 'Hello ' + name"
> 	}
> 	test: {
> 		map: "(doc) ->
> 	say_hello = require('views/lib/say_hello').callback
> 	emit(say_hello(doc._id), doc)"
> 	}
> }
> 
> When ran, it returns the following errors for each document:
> function raised exception (new TypeError("say_hello is not a function", "undefined", 5)) with doc._id
> 
> Here's the Javascript document which works just fine:
> {
> 	_id: "_design/test",
> 	language: "javascript",
> 	lib: {
> 		say_hello: "exports.callback = function(name) { return 'Hello ' + name }"
> 	}
> 	test: {
> 		map: "function(doc) { say_hello = require('views/lib/say_hello').callback; emit(say_hello(doc._id), doc) }"
> 	}
> }
> 
> Currently, I have all of my views in a real document defined in CoffeeScript and I'd really rather not convert them all to Javascript. Any help would be appreciated.
> 
> - Anthony


Re: CoffeeScript CommonJS Module Function

Posted by Anthony Kaufman <an...@gmail.com>.
OK thanks Dave, good tip.

Unless I'm missing something though, which has been definitely known to happen, it appears that it's not an issue with the CoffeeScript code itself. To me it looks like the CommonJS CoffeeScript functions don't work whereas their Javascript equivalents do.

Consider the following Javascript code:
{
	lib: {
		simple: "exports.callback = function() { return 'Simple' }"
	},
	test: {
		map: "function(doc) { emit(require('views/lib/simple').callback(), doc) }"
	}
}

As expected, "Simple" is emitted for the key of every document.

Now the equivalent CoffeeScript code:
{
	lib: {
		simple: "exports.callback = () -> 'Simple'"
	},
	test: {
		map: "(doc) -> emit(require('views/lib/simple').callback(), doc)"
	}
}

The view returns no results and an error for every document is printed to the log file. For example:
[info] [<0.7270.11>] OS Process #Port<0.96999> Log :: function raised exception (new TypeError("require(\"views/lib/simple\").callback is not a function", "undefined", 3)) with doc._id 8117

Anyway, hopefully more pieces to the puzzle.

Thanks,
Anthony

On Aug 19, 2013, at 1:27 PM, Dave Cottlehuber <dc...@jsonified.com> wrote:

> Hi Anthony,
> 
> I can't help personally on this but I suspect you might get more guidance
> on user@ where there's a larger group of potential coffeescripters about.
> 
> A+
> Dave
> 
> 
> On 19 August 2013 22:18, Anthony Kaufman <an...@gmail.com> wrote:
> 
>> Hi everyone, I just wanted to check back on this. Any help for me?
>> 
>> On Aug 16, 2013, at 2:10 PM, Jann Horn <ja...@thejh.net> wrote:
>> 
>>> On Fri, Aug 16, 2013 at 11:07:30PM +0200, Jann Horn wrote:
>>>> On Fri, Aug 16, 2013 at 02:03:03PM -0700, Anthony Kaufman wrote:
>>>>> I've been trying to define a CoffeeScript CommonJS Module Function
>> without success. I can translate the same function into Javascript and it
>> works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
>>>>> 
>>>>> Here's the CoffeeScript document:
>>>>> {
>>>>>    _id: "_design/test",
>>>>>    language: "coffeescript",
>>>>>    lib: {
>>>>>            say_hello: "exports.callback = (name) -> 'Hello ' + name"
>>>>>    }
>>>>>    test: {
>>>>>            map: "(doc) ->
>>>>>    say_hello = require('views/lib/say_hello').callback
>>>>>    emit(say_hello(doc._id), doc)"
>>>>>    }
>>>>> }
>>>> 
>>>> Isn't the indentation a bit broken there?
>>> 
>>> Sorry, nevermind.
>> 
>> 


Re: CoffeeScript CommonJS Module Function

Posted by Dave Cottlehuber <dc...@jsonified.com>.
Hi Anthony,

I can't help personally on this but I suspect you might get more guidance
on user@ where there's a larger group of potential coffeescripters about.

A+
Dave


On 19 August 2013 22:18, Anthony Kaufman <an...@gmail.com> wrote:

> Hi everyone, I just wanted to check back on this. Any help for me?
>
> On Aug 16, 2013, at 2:10 PM, Jann Horn <ja...@thejh.net> wrote:
>
> > On Fri, Aug 16, 2013 at 11:07:30PM +0200, Jann Horn wrote:
> >> On Fri, Aug 16, 2013 at 02:03:03PM -0700, Anthony Kaufman wrote:
> >>> I've been trying to define a CoffeeScript CommonJS Module Function
> without success. I can translate the same function into Javascript and it
> works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
> >>>
> >>> Here's the CoffeeScript document:
> >>> {
> >>>     _id: "_design/test",
> >>>     language: "coffeescript",
> >>>     lib: {
> >>>             say_hello: "exports.callback = (name) -> 'Hello ' + name"
> >>>     }
> >>>     test: {
> >>>             map: "(doc) ->
> >>>     say_hello = require('views/lib/say_hello').callback
> >>>     emit(say_hello(doc._id), doc)"
> >>>     }
> >>> }
> >>
> >> Isn't the indentation a bit broken there?
> >
> > Sorry, nevermind.
>
>

Re: CoffeeScript CommonJS Module Function

Posted by Anthony Kaufman <an...@gmail.com>.
Hi everyone, I just wanted to check back on this. Any help for me?

On Aug 16, 2013, at 2:10 PM, Jann Horn <ja...@thejh.net> wrote:

> On Fri, Aug 16, 2013 at 11:07:30PM +0200, Jann Horn wrote:
>> On Fri, Aug 16, 2013 at 02:03:03PM -0700, Anthony Kaufman wrote:
>>> I've been trying to define a CoffeeScript CommonJS Module Function without success. I can translate the same function into Javascript and it works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
>>> 
>>> Here's the CoffeeScript document:
>>> {
>>> 	_id: "_design/test",
>>> 	language: "coffeescript",
>>> 	lib: {
>>> 		say_hello: "exports.callback = (name) -> 'Hello ' + name"
>>> 	}
>>> 	test: {
>>> 		map: "(doc) ->
>>> 	say_hello = require('views/lib/say_hello').callback
>>> 	emit(say_hello(doc._id), doc)"
>>> 	}
>>> }
>> 
>> Isn't the indentation a bit broken there?
> 
> Sorry, nevermind.


Re: CoffeeScript CommonJS Module Function

Posted by Jann Horn <ja...@thejh.net>.
On Fri, Aug 16, 2013 at 11:07:30PM +0200, Jann Horn wrote:
> On Fri, Aug 16, 2013 at 02:03:03PM -0700, Anthony Kaufman wrote:
> > I've been trying to define a CoffeeScript CommonJS Module Function without success. I can translate the same function into Javascript and it works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
> > 
> > Here's the CoffeeScript document:
> > {
> > 	_id: "_design/test",
> > 	language: "coffeescript",
> > 	lib: {
> > 		say_hello: "exports.callback = (name) -> 'Hello ' + name"
> > 	}
> > 	test: {
> > 		map: "(doc) ->
> > 	say_hello = require('views/lib/say_hello').callback
> > 	emit(say_hello(doc._id), doc)"
> > 	}
> > }
> 
> Isn't the indentation a bit broken there?

Sorry, nevermind.

Re: CoffeeScript CommonJS Module Function

Posted by Jann Horn <ja...@thejh.net>.
On Fri, Aug 16, 2013 at 02:03:03PM -0700, Anthony Kaufman wrote:
> I've been trying to define a CoffeeScript CommonJS Module Function without success. I can translate the same function into Javascript and it works fine. I'm using CouchDB 1.3.1 on Mac OS X Lion.
> 
> Here's the CoffeeScript document:
> {
> 	_id: "_design/test",
> 	language: "coffeescript",
> 	lib: {
> 		say_hello: "exports.callback = (name) -> 'Hello ' + name"
> 	}
> 	test: {
> 		map: "(doc) ->
> 	say_hello = require('views/lib/say_hello').callback
> 	emit(say_hello(doc._id), doc)"
> 	}
> }

Isn't the indentation a bit broken there?