incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alux <>
Subject Create document with update handler (without sending an id)
Date Wed, 28 Jul 2010 08:01:36 GMT

after some funny result I now am able to create documents with an update
handler. I want to use usual couchy ids, and didnt know how to get the (in
one step, that is), so I did created one in JavaScript as below. Is this
sensible? I'm a newbee, so comments are very welcome.

Kind regards, alux


function(doc, req) {
            start({'headers': {'Content-Type': 'text/html'}});
            var maxStr = 'ffffffffff';
            var max = parseInt(maxStr, 16);
            var r1=Math.floor(Math.random()*max);
            var r2=Math.floor(Math.random()*max);
            var r3=Math.floor(Math.random()*max);
            var r4=Math.floor(Math.random()*max);
            var rstr = r1.toString(16) + r2.toString(16) + r3.toString(16) +
            var id = rstr.substr(0,32);
            doc = {_id: id};
            doc.type = 'note';
            var message = '<html><body><h3>Created new document with id='
id + '</h3>' +
            '<a href=\'' +id+ '\'>Enter the new
            return [doc, message];


1. This update handler shall be called from a browser with HTTP POST from a
form, it returns HTML for the browser.
2. The four steps of random generation are because Math.random generates not
very long numbers, and toString() removes leading zeros. So this should be
rather safe.

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message