Is there a way to include additional fields using the update handler in Couchdb? I'm trying to insert the author (with a validation function to verify that the user is logged in) and the id when creating a new document via an update handler.
The author's name can be accessed from req.userCtx.Name, the data sent is in req.body, and I'm utilizing req.uuid as the _id
"updates": {
"new": "function(doc,req) { var message = req.uuid; return [ { _id : req.uuid, "author" : req.userCtx.name, data : req.body}, message]}"
}
Data is being transmitted through a curl POST
$ cat test.json
{"data" : {"name" : "myname","dob" : "myDOB"}}
This is how the data appears when processed by the update handler
{"_id":"a018fed749d64f5db754b39af803a88f","_rev":"1-939793c36d2bfe4de86f808fab056959","author":"admin","data":"{\"name\" : \"myname\",\"dob\" : \"myDOB\"}"}
If I perform a standard POST (not via an update handler), the output looks like this:
{"_id":"a018fed749d64f5db754b39af803b621","_rev":"1-e44f0471e1df1018439fee3681b49547","data":{"name":"myname","dob":"myDOB"}}
What could be the error in my approach?
EDIT
After spending several hours searching and then posting questions, I finally found the solution.
Reference link -
{ "new": "function(doc,req) { var data = JSON.parse(req.body); data['_id'] = req.uuid;data.author = req.userCtx.name; message = req.uuid; return [ data, message]}" }
Now the document includes id and author information..
{"_id":"a018fed749d64f5db754b39af80406b7","_rev":"1-c486b02d6f320eb15e6115e71b3f02cc","data":{"name":"myname","dob":"myDOB"},"author":"admin"}