2017-05-25 39 views
0

我想按照這個教程來篩選pouchdb和CouchDB的數據庫錯誤過濾功能創建的CouchDB的

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

問題是,當我嘗試創建過濾功能之間的複製Fauxton webapp。在我創建的數據庫中,我點擊設計文件>新建文檔,然後粘貼功能:

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

,當我點擊創建文檔按鈕,它崩潰。 JavaScript控制檯說

未捕獲的SyntaxError:意外的標記ü在JSON第61位 在JSON.parse()來 在t.checkDocIsValid(https://127.0.0.1:6984/_utils/dashboard.assets/js/bundle-b8e0ba71119195edb7ec64b98f53d9b9.js:529:19481) 在t.saveDoc(https://127.0.0.1:6984/_utils/dashboard.assets/js/bundle-b8e0ba71119195edb7ec64b98f53d9b9.js:529:19056) ...

如何在couchDB中創建過濾函數?也許這不是程序,或者我必須在另一個dababase上創建它。在此先感謝

回答

2

所以你要做的是使用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  }" 
    } 
} 
+0

謝謝你Alexis,那有效! – jmann