Return _rev and _id in CouchDB _update function?
Is there a way to return the newly created document's _rev and _id field to the cli开发者_开发问答ent from an _update function?
You can, however the solution is not perfect.
You already know the document _id
in the update function. Either you are computing it yourself, or using user input, or if you wish to let CouchDB produce an ID automatically, then use the req.uuid
value.
function(doc, req) {
// An example _update function.
var id;
id = "Whatever"; // Pick one yourself, or...
id = req.query.id; // Let the user specify via ?id=whatever, or...
id = req.body; // Let the user specify via POST or PUT body, or...
id = req.uuid; // Use a random UUID from CouchDB
var doc = {"_id":id, "other_stuff":"Whatever other data you have"};
log("Document _id will be: " + doc._id);
return([doc, {json: {"success":true, "doc":doc}]);
}
Unfortunately, you do not know the _rev
in the show function. However, CouchDB will send it to the client in the HTTP header X-Couch-Update-NewRev
.
For example:
HTTP/1.1 201 Created
X-Couch-Update-NewRev: 1-967a00dff5e02add41819138abb3284d
Server: CouchDB/1.1.0 (Erlang OTP/R14B03)
Date: Tue, 12 Jul 2011 06:09:34 GMT
Content-Type: application/json
Content-Length: 14
{"stuff":true}
精彩评论