incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Higham <mar...@ocasta.co.uk>
Subject Re: Create document with update handler (without sending an id)
Date Wed, 28 Jul 2010 08:21:23 GMT
I had the same problem and was about to embark on a similar solution when I
discovered that the req object passed into the update handler contains a
uuid for you to use. So all you need to do is

function(doc, req) {
  var newDoc = JSON.parse(req.body);
  if (!doc){
    newDoc._id = req.uuid;
...


Martin



On 28 July 2010 09:01, Alux <aluxob@googlemail.com> wrote:

> Hello,
>
> 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) +
> r4.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
> document.</a></body></html>';
>            return [doc, message];
>        }
>
> Remarks:
>
> 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.
>

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