Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CB25810636 for ; Thu, 12 Dec 2013 14:24:27 +0000 (UTC) Received: (qmail 67281 invoked by uid 500); 12 Dec 2013 14:24:25 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 67255 invoked by uid 500); 12 Dec 2013 14:24:25 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 67247 invoked by uid 99); 12 Dec 2013 14:24:24 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Dec 2013 14:24:24 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of james.dingwall@zynstra.com designates 213.199.154.75 as permitted sender) Received: from [213.199.154.75] (HELO emea01-db3-obe.outbound.protection.outlook.com) (213.199.154.75) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 12 Dec 2013 14:24:16 +0000 Received: from DBXPRD0310HT005.eurprd03.prod.outlook.com (10.255.65.168) by DBXPR03MB558.eurprd03.prod.outlook.com (10.141.232.147) with Microsoft SMTP Server (TLS) id 15.0.837.10; Thu, 12 Dec 2013 14:23:54 +0000 Received: from [192.168.10.196] (193.63.64.25) by pod51013.outlook.com (10.255.65.168) with Microsoft SMTP Server (TLS) id 14.16.383.1; Thu, 12 Dec 2013 14:23:54 +0000 Message-ID: <52A9C6F7.8020108@zynstra.com> Date: Thu, 12 Dec 2013 14:23:51 +0000 From: James Dingwall User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0 SeaMonkey/2.22.1 MIME-Version: 1.0 To: Subject: Re: Update functions and require() References: <52A9C000.3060005@zynstra.com> In-Reply-To: <52A9C000.3060005@zynstra.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [193.63.64.25] X-Forefront-PRVS: 0058ABBBC7 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019001)(164054003)(199002)(24454002)(51704005)(189002)(33656001)(50466002)(56816005)(80022001)(81816001)(90146001)(47776003)(69226001)(15975445006)(83506001)(66066001)(81686001)(63696002)(85306002)(47976001)(49866001)(56776001)(76796001)(81342001)(87936001)(59766001)(74706001)(54316002)(47736001)(74662001)(15202345003)(46102001)(80316001)(76786001)(83322001)(74366001)(19580395003)(79102001)(31966008)(50986001)(80976001)(54356001)(36756003)(64126003)(47446002)(74502001)(4396001)(23676002)(51856001)(81542001)(74876001)(85852003)(76482001)(83072002)(77982001)(53806001)(59896001);DIR:OUT;SFP:1102;SCL:1;SRVR:DBXPR03MB558;H:DBXPRD0310HT005.eurprd03.prod.outlook.com;CLIP:193.63.64.25;FPR:;RD:InfoNoRecords;A:1;MX:1;LANG:en; X-OriginatorOrg: zynstra.com X-Virus-Checked: Checked by ClamAV on apache.org James Dingwall wrote: > Hi, > > I am experiencing some problems with update functions which I use to > create documents in CouchDB when they are using require() to load a > common library. It seems that after publishing the design document > then the first update function to be called works well but any > subsequent ones do not call require(). With the log() call in the > require I see this appear in the call to the first update but it does > not appear with any others, they print a stack trace such as: > [Thu, 12 Dec 2013 13:20:55 GMT] [error] [<0.6458.0>] httpd 500 error > response: > {"error":"render_error","reason":"function raised error: (new > ReferenceError(\"docUtil is not defined\", \"updates.template\", 54)) > stacktrace: (null,[object Object])@updates.template:54 > runUpdate(<<>>),[object Object],[object > Array])@/usr/local/share/couchdb/server/main.js:1033 > (\"_design/provision\",[object Array],[object > Array])@/usr/local/share/couchdb/server/main.js:1517 > ()@/usr/local/share/couchdb/server/main.js:1562 > @/usr/local/share/couchdb/server/main.js:1573 > "} > > As far as I can tell by require()d code is ok since the same structure > is working for other things. What it seems to me is that it is loaded > by CouchDB for the first update function and is then recorded as > loaded but a second update function needing the same code has its > require ignored but cannot reference the in memory copy produce by the > require from the first update. > > I have tested versions CouchDB 1.3.1, 1.4.0 and 1.5.0 all with the > same behaviour. I am running on Ubuntu precise with esl-erlang > 1:16.b.3-1. > > Any suggestions on where to look next? > > Thanks, > James > > > As an example: > > The updates are all of the form: > function(doc, req) { > require('views/lib/docUtil'); > > if(!doc) { > doc = {some: defaults} > } > > // some other code > > doc.meta = docUtil.metaCreate(req); > > return([doc, JSON.stringify(doc, undefined, 2)]); > } > > views/lin/docUtil is similar too: > (function(global) { > > var _metaCreate = function(req) { > ... > }; > > var _metaModify = function(req) { > ... > }; > > log('docUtil loaded') > global.docUtil = { > metaCreate = _metaCreate, > metaModify = metaModify > }; > > })(exports.docUtil = this) > Here is a complete test case design document which demonstrates the problem. { "_id": "_design/ptest", "updates": { "two": "function(doc, req) {\n\trequire('views/lib/docUtil');\n\n\tif(!doc) {\n\t\tdoc = {};\n\t}\n\n\tdoc._id = 'two';\n\n\tdoc.meta = docUtil.metaCreate();\n\n\treturn([doc, JSON.stringify(doc, undefined, 2)]);\n}", "one": "function(doc, req) {\n\trequire('views/lib/docUtil');\n\n\tif(!doc) {\n\t\tdoc = {};\n\t}\n\n\tdoc._id = 'one';\n\n\tdoc.meta = docUtil.metaCreate();\n\n\treturn([doc, JSON.stringify(doc, undefined, 2)]);\n}" }, "language": "javascript", "views": { "lib": { "docUtil": "(function(global) {\n\nvar _metaCreate = function() {\n\treturn({\n\t\ta: 'create',\n\t\tb: 'bbbb'\n\t});\n};\n\nvar _metaModify = function() {\n\treturn({\n\t\ta: 'modify',\n\t\tb: 'bbbb'\n\t});\n};\n\nglobal.docUtil = {\n\tmetaCreate: _metaCreate,\n\tmetaModify: _metaModify\n};\n\nlog('require docUtil');\n\n})(exports.docUtil = this)" } }, "type": "design/ptest" } Call the update functions like: curl -X POST http://couchdb:5984/zydev/_design/ptest/_update/one - creates a document curl -X POST http://couchdb:5984/zydev/_design/ptest/_update/two - fails