在couchdb上过滤函数创建中的错误

时间:2017-05-25 03:03:40

标签: couchdb

我正在尝试按照本教程过滤pouchdb和couchdb数据库之间的复制

https://pouchdb.com/2015/04/05/filtered-replication.html

问题是我尝试在Fauxton webapp中创建过滤后的功能。在我创建的数据库中,单击Design Document>新文档然后粘贴功能:

  {
     "_id": "_design/app",
     "filters": {
       "by_agent": function(doc, req) {
         return doc.agent === req.query.agent;
       }.toString()
     }
  }

当我单击“创建文档”按钮时,它会崩溃。 javascript控制台说

Uncaught SyntaxError:位置61的JSON中出现意外的标记u     在JSON.parse()     at t.checkDocIsValid(https://127.0.0.1:6984/_utils/dashboard.assets/js/bundle-b8e0ba71119195edb7ec64b98f53d9b9.js:529:19481)     at t.saveDoc(https://127.0.0.1:6984/_utils/dashboard.assets/js/bundle-b8e0ba71119195edb7ec64b98f53d9b9.js:529:19056) ...

如何在couchDB中创建过滤后的功能?也许这不是程序,或者我必须在另一个dababase上创建它。提前致谢

1 个答案:

答案 0 :(得分:3)

因此,您尝试做的是使用JavaScript代码创建视图。因此,Fauxton仅将JSON作为文档。

以下是如何从JavaScript代码段中获取JSON:



//The snippet you had was a JavaScript object
//Even if it seems like a JSON object, there is a function() declaration followed by a .toString()
//By doing so, it easier to write functions instead of writing them in a raw string.

var javascriptObject = {
     "_id": "_design/app",
     "filters": {
       "by_agent": function(doc, req) {
         return doc.agent === req.query.agent;
       }.toString()
     }
  }
  console.info("You should use the following string in your Fauxton Editor:");
  console.log(JSON.stringify(javascriptObject));




您应该使用以下字符串代替您尝试的JavaScript代码段:

{
  "_id": "_design/app",
  "filters": {
    "by_agent": "function (doc, req) {\n         return doc.agent === req.query.agent;\n       }"
  }
}