所以我正在尝试创建一个设计文档CouchDB
还有一个小问题。这是一个机器人团队侦察系统。设计文档应该检查用户是否具有“marea”角色。如果他们这样做,则允许他们将文档输入到数据库中。然后设计文档应该采用 JSON 字段“points”并在“team”字段匹配时对其进行平均。平均值应合并回数据库。如果用户没有“marea”角色,那么他们应该被限制输入任何文档。请让我知道您对以下代码的看法,我还想就收到的错误消息提供一些建议。我可以很好地保存设计文档,但它的功能有点不确定。即使该用户具有“marea”角色,它也可以防止用户创建具有“points”值的新文档。
Save failed: Expression does not eval to a function. (function(newDoc, oldDoc, userCtx, secObj) { if ((userCtx.roles.indexOf("2018marea") !==
-1) || (userCtx.name == oldDoc.name)) { if (!doc) { if ("id" in req && req["id"]) { return [{"_id": req["id"]}, "New Doc"] emit(null, "points") var pointsArray = ["points"], thisTotal = 0, thisAverage = 0; for(var i = 0;i < pointsArray.length; i++) { thisTotal+ = pointsArray[i]; } thisAverage = (thisTotal/pointsArray.length); } return [null, "Empty Database."] } doc["New Doc"] = thisAverage; doc["edited_by"] = req["userCtx"]["name"] return [doc, "Edited Data."] } else { return [null, "Cannot Overwrite Data"] } } )
如何解决此问题,以便设计文档执行其预期功能?
{
"_id": "_design/marea",
"language": "javascript",
"validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) {\r\n\r\n if ((userCtx.roles.indexOf(\"2018marea\") !== -1) || (userCtx.name == oldDoc.name)) { \r\n \r\n if (!doc) {\r\n \r\n if (\"id\" in req && req[\"id\"]) {\r\n \r\n return [{\"_id\": req[\"id\"]}, \"New Doc\"] \r\n emit(null, \"points\")\r\n var pointsArray = [\"points\"], thisTotal = 0, thisAverage = 0;\r\n for(var i = 0;i < pointsArray.length; i++) {\r\n \r\n thisTotal+ = pointsArray[i];\r\n \r\n }\r\n \r\n thisAverage = (thisTotal/pointsArray.length); \r\n \r\n }\r\n \r\n return [null, \"Empty Database.\"]\r\n \r\n }\r\n \r\n doc[\"New Doc\"] = thisAverage;\r\n doc[\"edited_by\"] = req[\"userCtx\"][\"name\"] \r\n return [doc, \"Edited Data.\"] \r\n\r\n } else {\r\n \r\n return [null, \"Cannot Overwrite Data\"]\r\n \r\n }\r\n } "
}