简体   繁体   English

调用Cloudant / CouchDB设计文档中的外部javascript函数

[英]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. 我有一个用JavaScript(其他人写了这个函数)的Cloudant数据库设计文档。 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 在本文档中,我想首先调用JSON.minify,我在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)); 更新功能的代码如下..我想知道如何根据提供的链接中的建议从代码中调用JSON.minify:JSON.parse(JSON.minify(str));

Where I currently have _ref = JSON.parse(reqBody) I want to use _ref = JSON.prase(JSON.minify(reqBody)); 我目前有_ref = JSON.parse(reqBody)的地方,我想使用_ref = JSON.prase(JSON.minify(reqBody));

Can someone tell me how I can call this external code from a design doc in Cloudant. 有人可以告诉我如何从Cloudant中的设计文档中调用此外部代码。 (Cloudant works very similar to CouchDB in most cases, so I think it may be the same answer) (在大多数情况下,Cloudant的工作方式与CouchDB非常相似,因此我认为答案可能是相同的)

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 . 您应该能够在更新功能的顶部包含源代码 ,或者将其作为CommonJS模块加载。

Have you tried either one? 你有没有尝试过?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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