incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From J Chris Anderson <jch...@gmail.com>
Subject Re: How to change JSON before storing
Date Tue, 27 Apr 2010 14:54:05 GMT

On Apr 27, 2010, at 7:48 AM, Andreas Köberle wrote:

> I don't get it. Logging req and doc shows me that doc is null, but
> req.body has the json:
> 

yes. that is the idea.

CouchDB doesn't know if the request is JSON or CSV or whatever, so it doesn't try to parse
it for you. Since you don't have a doc with that id already, doc is null.

So you need to do something like:

function(doc, req){

var newDoc = JSON.parse(req.body);
newDoc.date = new Date(); 

return [newDoc, "yay it worked!"];

}

> 1> [info] [<0.930.0>] OS Process :: req.info: [object Object]
> 1> [info] [<0.930.0>] OS Process :: req.verb: POST
> 1> [info] [<0.930.0>] OS Process :: req.path:
> yslow,_design,yslow,_update,addDate
> 1> [info] [<0.930.0>] OS Process :: req.query: [object Object]
> 1> [info] [<0.930.0>] OS Process :: req.headers: [object Object]
> 1> [info] [<0.930.0>] OS Process :: req.body:
> {"w":50433,"o":94,"u":"http%3A%2F%2Fwww.google.de%2F","r":6,"s":"","i":"ydefault","lt":401,"comps":
> [{"type":"doc","url":"http%3A%2F%2Fwww.google.de%2F","size":11944,"resp":53,"gzip":"4730","cr":481},{"type":"js","url":"http%3A%2F%2Fwww.google.d
> e%2Fextern_js%2Ff%2FCgJkZRICZGUrMAo4Y0AdLCswDjgNLCswFjgXLCswFzgGLCswGDgFLCswGTgcLCswHTgrLCswITg1QAEsKzAlOMqIASwrMCY4CiwrMCc4BCwrMCo4BCwrMCs4DCwrM
> Dw4AiwrMEA4DywrMEU4ASwrME44BCwrMFE4ACyAAg0%2F_urb4V0rNT4.js","size":73479,"resp":4,"gzip":"27110","expires":"2011/4/26"},{"type":"js","url":"http
> %3A%2F%2Fwww.google.de%2Fextern_chrome%2Febbd8e4827c4c9bc.js","size":13362,"resp":6,"gzip":"3816","expires":"2011/4/26"},{"type":"image","url":"h
> ttp%3A%2F%2Fwww.google.de%2Fintl%2Fde_de%2Fimages%2Flogo.gif","size":9121,"resp":9,"expires":"2010/4/27"},{"type":"image","url":"http%3A%2F%2Fwww
> .google.de%2Fimages%2Fnav_logo8.png","size":5656,"resp":16,"expires":"2010/4/27"},{"type":"doc","url":"http%3A%2F%2Fclients1.google.de%2Fgenerate
> _204","size":0,"resp":28,"cr":481}]}
> 1> [info] [<0.930.0>] OS Process :: req.form: [object Object]
> 1> [info] [<0.930.0>] OS Process :: req.cookie: [object Object]
> 1> [info] [<0.930.0>] OS Process :: req.userCtx: [object Object]
> 1> [info] [<0.930.0>] OS Process :: doc: null
> 
> 
> 
> On 27 April 2010 15:58, Andreas Köberle <koeberle@gmail.com> wrote:
>> Ok there was an error in my design doc now it looks like:
>> 
>> {
>>   "_id": "_design/yslow",
>>   "_rev": "12-ad0d504e48808ef5643545e3817a62f8",
>>   "updates": {
>>       "addDate": "function(doc, req){doc.date = new Date(); return[doc,req]}"
>>   }
>> }
>> 
>> And I got another error: JavaScript function raised error: TypeError:
>> doc is null. Normal saving the json in http://exeter:5984/yslow works
>> well by the way.
>> 
>> On 27 April 2010 15:17, Andreas Köberle <koeberle@gmail.com> wrote:
>>> Mhmm it seems that POST work, but now I get another quirk.
>>> 
>>> this is my design doc:
>>> {
>>>   "_id": "_design/yslow",
>>>   "_rev": "9-a4ece33bca6f678cf1752907bfe8596c",
>>>   "updates": "{'addDate' : 'function(doc, req){doc.date = new Date();
>>> return[doc,req]}'}"
>>> }
>>> 
>>> this is my url:
>>> http://exeter:5984/yslow/_design/yslow/_update/addDate
>>> 
>>> this is the json that YSLOW sends:
>>> 
>>> var a = {"w":41480,"o":93,"u":"http%3A%2F%2Fwww.google.com%2Fsearch%3Fie%3DUTF-8%26oe%3DUTF-8%26sourceid%3Dnavclient%26gfns%3D1%26q%3Dyslow%2Bbeacon%2Bcouchdb","r":6,"s":"","i":"ydefault","lt":582,"comps":[
>>>        {
>>>                "type":"doc",
>>>                "url":"http%3A%2F%2Fwww.google.com%2Fsearch%3Fie%3DUTF-8%26oe%3DUTF-8%26sourceid%3Dnavclient%26gfns%3D1%26q%3Dyslow%2Bbeacon%2Bcouchdb",
>>>                "size":31534,
>>>                "resp":226,
>>>                "gzip":8407,
>>>                "cr":481
>>>        },
>>>        {
>>>                "type":"js",
>>>                "url":"http%3A%2F%2Fwww.google.com%2Fextern_js%2Ff%2FCgJkZSswCjhjQB0sKzAOOA0sKzAWOBcsKzAXOAYsKzAYOAUsKzAZOBwsKzAhODVAASwrMCU4yogBLCswJjgKLCswJzgELCswKjgELCswKzgMLCswPDgCLCswQDgPLCswRTgBLCswTjgELCswUTgALIACDQ%2FKQlSo2GWluY.js",
>>>                "size":57272,
>>>                "resp":24,
>>>                "gzip":"21189",
>>>                "expires":"2011/4/26"
>>>        },
>>>        {
>>>                "type":"cssimage",
>>>                "url":"http%3A%2F%2Fwww.google.com%2Fimages%2Fnav_logo8.png",
>>>                "size":5656,
>>>                "resp":24,
>>>                "expires":"2010/4/27"
>>>        },
>>>        {
>>>                "type":"image",
>>>                "url":"http%3A%2F%2Fwww.google.com%2Fimages%2Ficons%2Fsearch-16.gif",
>>>                "size":1005,
>>>                "resp":23,
>>>                "expires":"2010/4/27"
>>>        },
>>>        {
>>>                "type":"image",
>>>                "url":"http%3A%2F%2Fwww.google.com%2Fimages%2Fswxa.gif",
>>>                "size":5223,
>>>                "resp":22,
>>>                "expires":"2010/4/27"
>>>        },
>>>        {
>>>                "type":"doc",
>>>                "url":"http%3A%2F%2Fclients1.google.com%2Fgenerate_204",
>>>                "size":0,
>>>                "resp":null
>>>        }
>>> ]}
>>> 
>>> and this is the error I've got:
>>> 
>>> 1> [debug] [<0.11411.0>] OAuth Params: []
>>> 1> [debug] [<0.11411.0>] Minor error in HTTP request: {not_found,json_mismatch}
>>> 1> [debug] [<0.11411.0>] Stacktrace: [{couch_util,get_nested_json_value,2},
>>>             {couch_httpd_show,handle_doc_update_req,2},
>>>             {couch_httpd_db,do_db_req,2},
>>>             {couch_httpd,handle_request,5},
>>>             {mochiweb_http,headers,5},
>>>             {proc_lib,init_p_do_apply,3}]
>>> 1> [debug] [<0.11411.0>] httpd 404 error response:
>>>  {"error":"not_found","reason":"json_mismatch"}
>>> 
>>> 
>>> On 27 April 2010 14:48, Adam Kocoloski <kocolosk@apache.org> wrote:
>>>> No, validation functions cannot modify documents.  The usual reason given
is that they run during replication as well as during client updates.
>>>> 
>>>> Adam
>>>> 
>>>> On Apr 27, 2010, at 8:40 AM, Andreas Köberle wrote:
>>>> 
>>>>> Unfortunately _updates doesn't work for me cause YSLOW send with POST
>>>>> and I get a "Only DELETE,GET,HEAD,PUT allowed" error. Could I use
>>>>> validate_doc_update instead?
>>>>> 
>>>>> 
>>>>> On 27 April 2010 14:12, Adam Kocoloski <kocolosk@apache.org> wrote:
>>>>>> That's correct, Simon, _update handlers don't work with _bulk_docs
today.  Best,
>>>>>> 
>>>>>> Adam
>>>>>> 
>>>>>> On Apr 27, 2010, at 8:07 AM, Simon Metson wrote:
>>>>>> 
>>>>>>> Hi,
>>>>>>>       Is it possible to apply an update function to a bulk insert?
From reading that page it sounds like not...
>>>>>>> Cheers
>>>>>>> Simon
>>>>>>> 
>>>>>>> On 27 Apr 2010, at 11:05, Sebastian Cohnen wrote:
>>>>>>> 
>>>>>>>> The answer is yes, to make it short :)
>>>>>>>> 
>>>>>>>> http://wiki.apache.org/couchdb/Document_Update_Handlers
>>>>>>>> 
>>>>>>>> 
>>>>>>>> On 27.04.2010, at 11:56, Andreas Köberle wrote:
>>>>>>>> 
>>>>>>>>> Is there a way to add data to a JSON Object with couchdb,
before
>>>>>>>>> storing it. I've got data direct from YSlow firefox extension
but
>>>>>>>>> unfortunately without a timestamp.
>>>>>>>> 
>>>>>>> 
>>>>>> 
>>>>>> 
>>>> 
>>>> 
>>> 
>> 


Mime
View raw message