[英]Couchbase vs Cloudant CouchDB - differences with Javascript in reduce
[英]Call to external javascript function in Cloudant/CouchDB design doc
我有一個用JavaScript(其他人寫了這個函數)的Cloudant數據庫設計文檔。 創建此功能是為了更新文檔。 在本文檔中,我想首先調用JSON.minify,我在https://www.npmjs.com/package/jsonminify上找到了一些在線代碼
更新功能的代碼如下..我想知道如何根據提供的鏈接中的建議從代碼中調用JSON.minify:JSON.parse(JSON.minify(str));
我目前有_ref = JSON.parse(reqBody)的地方,我想使用_ref = JSON.prase(JSON.minify(reqBody));
有人可以告訴我如何從Cloudant中的設計文檔中調用此外部代碼。 (在大多數情況下,Cloudant的工作方式與CouchDB非常相似,因此我認為答案可能是相同的)
提前致謝!
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' }) ];
}
您應該能夠在更新功能的頂部包含源代碼 ,或者將其作為CommonJS模塊加載。
你有沒有嘗試過?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.