Re: Problem with commonjs in view

112 views
Skip to first unread message

Noah Diewald

unread,
May 23, 2013, 5:12:06 PM5/23/13
to us...@couchdb.apache.org
If anyone is curious about the "dumb" thing that I did, here it goes.

I created a view that referred to the exported function using a relative path.
This didn't work. I decided to try and debug the problem using the temporary
view editor. This didn't work because I guess that temporary views are special
somehow. I had thought that I'd tried updating the view to match this example,
without using a temporary view:

>> views: {
>> lib: {
>> blue: "exports.ninenine = 99;"
>> },
>> mymap: {
>> map: "function (doc) {emit(require('views/lib/blue').ninenine);}"
>> }
>> }

But I must not have done this because I tried it again and got it to work. Moral
of the story, temporary views are not a good place to debug commonjs problems.

On 05/23/2013 02:51 PM, Noah Diewald wrote:
> Oh. I cut and pasted badly. The two errors are not the same. The first should be
> "mod is undefined".
>
> On 05/23/2013 02:40 PM, Noah Diewald wrote:
>> I have a design document where I'm trying to use commonjs in a view. At this
>> point I've failed at trying to do the more complicated stuff that I intended to
>> use this functionality for. I'm just trying to do something simple so that I can
>> figure out why things aren't working for me. I'm following this wiki page fairly
>> closely: https://wiki.apache.org/couchdb/CommonJS_Modules
>>
>> I have a views property that looks like this:
>>
>> views: {
>> lib: {
>> blue: "exports.ninenine = 99;"
>> },
>> mymap: {
>> map: "function (doc) {emit(require('../lib/blue').ninenine);}"
>> }
>> }
>>
>> I get errors that look like the following for every document in the log when
>> attempting to run the view in the temporary view interface in futon:
>>
>> OS Process #Port<0.3115> Log :: function raised exception (new
>> TypeError("mod.current is null", "/usr/share/couchdb/server/main.js", 1125))
>> with doc._id cccb3bb779549b28b2e48eb628934107
>>
>> So it looks like relative paths don't work but if I use the property path as
>> shown in the wiki:
>>
>> views: {
>> lib: {
>> blue: "exports.ninenine = 99;"
>> },
>> mymap: {
>> map: "function (doc) {emit(require('views/lib/blue').ninenine);}"
>> }
>> }
>>
>> I see this error in the log:
>>
>> OS Process #Port<0.3115> Log :: function raised exception (new
>> TypeError("mod.current is null", "/usr/share/couchdb/server/main.js", 1125))
>> with doc._id cccb3bb779549b28b2e48eb628934107
>>
>> I haven't seen if the errors are any different when I don't try to get the map
>> function to run through futon but it isn't working either way.
>>
>> This looks to be particular to the views. I am using commonjs with both updates
>> and validate_doc_update in the same design document and they are working properly.
>>
>> I am using the Arch Linux package couchdb 1.2.2-3. I hope I'm just making some
>> type of dumb mistake that someone can point out. Any help would be very appreciated.
>>
>> Noah
>>
>>
>

signature.asc

Noah Diewald

unread,
May 23, 2013, 3:51:06 PM5/23/13
to us...@couchdb.apache.org
signature.asc

Noah Diewald

unread,
May 23, 2013, 3:40:45 PM5/23/13
to us...@couchdb.apache.org
signature.asc

Travis Paul

unread,
May 24, 2013, 9:50:15 AM5/24/13
to us...@couchdb.apache.org
Forgive me for not reading the whole email and ignore this if I missed some
critical information. But if you're tring to import a library in a view,
you can't. Views must be side-effect free. Someone please correct me if I'm
mistaken.

Robert Newson

unread,
May 24, 2013, 10:25:07 AM5/24/13
to us...@couchdb.apache.org
I can confirm you are wrong. You can require() into map functions.

Sent from my iPhone

Travis Paul

unread,
May 24, 2013, 10:29:10 AM5/24/13
to us...@couchdb.apache.org
Thanks Robert, Sorry Noah,
Has this always been the case? I was always under the impression that you
could only use require in validate, list, and show functions.

Robert Newson

unread,
May 24, 2013, 10:36:16 AM5/24/13
to us...@couchdb.apache.org
Since the introduction of commonjs, yes. The libs are positioned under views so that we can include the code in the checksum for the reason you mentioned.

Sent from my iPhone

Travis Paul

unread,
May 24, 2013, 10:46:50 AM5/24/13
to us...@couchdb.apache.org
Awesome, I guess I overlooked that.
I believe I now have some abused list functions to go refactor :)

Troy Martin

unread,
May 25, 2013, 8:54:42 PM5/25/13
to us...@couchdb.apache.org
If I want to use moment.js in a view, can I just replace " with ' in the minified source code (https://raw.github.com/timrwood/moment/2.0.0/min/moment.min.js), and use Futon to save the source code as a string under views.lib? Would I then be able to access it a view by calling require('views/lib/moment')? Sounds like Kanso would make this easier, but I don't want to install it just for this if I don't have to.

Thanks,

Troy

Jim Klo

unread,
May 26, 2013, 2:59:20 AM5/26/13
to <user@couchdb.apache.org>
Yep. That sounds about right. I've got a non-kanso couchapp that does exactly that.

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages