couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Rudolph <m.rudo...@email.de>
Subject Re: Validate Document Update - Change document
Date Fri, 10 Nov 2017 07:23:39 GMT
Hi Ronny,

I don’t think that you can do this within a Validate Doc update function. CouchDB has for
this case the Update function (http://docs.couchdb.org/en/latest/ddocs/ddocs.html#update-functions
<http://docs.couchdb.org/en/latest/ddocs/ddocs.html#update-functions>), which allows
you to change documents
before they are saved to the database.


> Am 09.11.2017 um 21:34 schrieb Ronny Berndt <ronny@kioskkinder.com>:
> 
> Hi,
> 
> is it possible to change a document before an insert?
> 
> I would like to modify all keys of an object to lower.
> Is this possible?
> 
> My JavaScript function so far is:
> 
> function(newDoc, oldDoc, userCtx) {
> 	var objectKeysToLowerCase = function (origObj) {
>            return Object.keys(origObj).reduce(function (newObj, key) {
>              var val = origObj[key];
>              var newVal = (typeof val === 'object') ? objectKeysToLowerCase(val) : val;
>              newObj[key.toLowerCase()] = newVal;
>              return newObj;
>            }, {});
>        }
> 	newDoc = objectKeysToLowerCase(oldDoc);
> }
> 
> I get the following error message: 
> 
> Save failed: Expression does not eval to a function. (function(newDoc, oldDoc, userCtx)
{ var objectKeysToLowerCase = function (origObj) { return Object.keys(origObj).reduce(function
(newObj, key) { var val = origObj[key]; var newVal = (typeof val === 'object') ? objectKeysToLowerCase(val)
: val; newObj[key.toLowerCase()] = newVal; return newObj; }, {}); } newDoc = JSON.stringify(objectKeysToLowerCase(oldDoc));
})
> 


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