0

So I'm trying to create a design doc in CouchDB and there's a slight problem. This is for a robotics team scouting system. The design doc is supposed to check if a user has the role "marea". If they do, then they are allowed to input a document into the database. Then the design doc should take the JSON field "points" and average it if the "team" fields match. The averaged value should be merged back into the database. If the user does not have the "marea" role, then they should be restricted from inputting any docs. Please let me know what you think of the below code, and I would also like some advice on the error message I get. I'm able to save the design doc just fine, but its functioning is a little iffy. It prevents the user from creating a new doc with a "points" value even when that user has the role "marea". The exact error message is:

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"] } } )

How do I fix this problem so that the design doc carries out its intended function?

{
  "_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  } "
}
Alexis Côté
  • 3,670
  • 2
  • 14
  • 30
ShashJar
  • 31
  • 3

1 Answers1

2

The error is pretty obvious: You function is not valid

  1. There's a space between + = : { thisTotal+ = pointsArray[i]; }
  2. You forgot some semi colon between few statements:

"New Doc"] ; emit(null, "points"); var pointsArray

This should work better:

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"]
  }
}
Alexis Côté
  • 3,670
  • 2
  • 14
  • 30