简体   繁体   中英

Call to external javascript function in Cloudant/CouchDB design doc

I have a design document written in javascript (someone else wrote this function) for a Cloudant database. This function is created to update a document. Within this document I want to first make a call to JSON.minify which I have found some code for online at https://www.npmjs.com/package/jsonminify

The code for the update function is below.. and I want to know how to make a call to JSON.minify from the code as suggested within the link provided: JSON.parse(JSON.minify(str));

Where I currently have _ref = JSON.parse(reqBody) I want to use _ref = JSON.prase(JSON.minify(reqBody));

Can someone tell me how I can call this external code from a design doc in Cloudant. (Cloudant works very similar to CouchDB in most cases, so I think it may be the same answer)

Thanks in advance!

function(doc, req) {  
if (!doc) {   
return [doc, JSON.stringify({ status: 'failed'      })];  
} 

var reqBody=req.body;
_ref = JSON.parse(reqBody);  


for (k in _ref) {  

v = _ref[k];   

if (k[0] === '/'){     
nestedDoc = doc;
nestedKeys = k.split('/');
_ref1 = nestedKeys.slice(1, -1);
for (_i = 0, _len = _ref1.length; _i < _len; _i++){
    nestedKey = _ref1[_i];
    nestedDoc = ((_ref2 = nestedDoc[nestedKey]) != null ? _ref2 : nestedDoc[nestedKey] = {});
}    
k = nestedKeys.slice(-1)[0];

if (v === '__delete__'){ 
    delete nestedDoc[k];     
}
continue;
}    
if (v === '__delete__'){      delete doc[k];    } 
else{      doc[k] = v;    }  }  

return [ doc, JSON.stringify({ status: 'success'    })  ];
}

You should be able to either include the source code at the top of your update function, or load it as a CommonJS module .

Have you tried either one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM