couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Heike Bernhard <heikebernh...@gmx.de>
Subject Re: Validate_doc_update required fields - can't delete documents
Date Sat, 09 Jan 2010 17:35:14 GMT
Chris Anderson wrote:
> On Fri, Jan 8, 2010 at 3:25 PM, Heike Bernhard <heikebernhard@gmx.de> wrote:
>> Heike Bernhard wrote:
>>> Hello,
>>>
>>> I'm new to CouchDB and I've just wrote my first validate function
>>> (thanks to the book "CouchDB: The definitive guide"). But now I have a
>>> problem, when I want to delete documents.
>>>
>>> For each document there are two fields required (firstName and lastName)
>>> and my function looks like this:
>>>
>>> "validate_doc_update": "function(newDoc, oldDoc, userCtx)
>>> {
>>>   function require(field, message)
>>>   {
>>>     message = message || \"Document requires the field \" + field;
>>>     if (!newDoc[field]) throw({forbidden : message});
>>>   };
>>>   if (newDoc)
>>>   {
>>>     require(\"lastName\");require(\"firstName\");
>>>   }
>>> }"
>>>
> 
> You can check for the _deleted field and short-circut validation
> before it requires the other fields.
> 
> if (newDoc._deleted && oldDoc.author == userCtx.name) return;
> 
> I'm also checking to see that the user is allowed to delete, in this case.
> 
> You're right, the book could be more clear on this. I'm not sure if
> there's time to add to the code samples before printing, but maybe
> there is.
> 
> Chris
> 

Hello Chris,

thank you for your fast response. This helped me to understand how this
works.

I've also cloned sofa and had the same problem with deleting documents.
But now it's clear. I wouldn't blame the book. Nevertheless more code
samples are always a good thing for newbies like me :)

Heike


>>> So if I want to delete a document, I get an error, because of the
>>> required fields:
>>>
>>> user@computer:~$ curl -X DELETE
>>> http://username:password@localhost:5984/testdb/foo?rev=6-5f2e9784c354bdf95e2d851f7f87d045
>>> {"error":"forbidden","reason":"Document requires the field lastName"}
>>>
>>> What am I missing?
>>> Thanks for any hints.
>>>
>>> Heike
>>>
>>>
>> Sorry, forgot to mention:
>> I'm using CouchDB 0.10.0 on Ubuntu 9.04
>>
> 
> 
> 


Mime
View raw message