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 <[email protected]>: > > 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)); }) >
