Sync fn exception: SyntaxError: Unexpected token


#1

I am having trouble with the sync function. Do someone know what is wrong with the sync function?
On Android I am creating the following document, which I try to sync to the sync gateway

doc = map[_rev:1-0afc211e2d8b613023792956b71069ea title:UsersTestDocumenttitle _id:e232a5c1-28ef-4

630-b8b5-84819bc20d2d created_at:2015-06-10T20:48:21.802Z channels:main_channel writers:[skipxxx] type:UsersTestDocument creator:skipxxx _revisions:map[start:%!s(float64=1) ids:[0afc211
e2d8b613023792956b71069ea]]

I successfully synced this document from with the simplest configuration

    {
   "log": ["CRUD", "CRUD+", "HTTP", "HTTP+", "Access", "Cache", "Changes", "Changes+"],
   "interface":":4984",
   "adminInterface":":4985",
   "facebook":{
      "register":true
   },
   "databases":{
      "gw":{
         "server":"http://localhost:8091",
         "bucket":"sync_gateway",
         "sync":`function(doc) {channel(doc.channels);}`,
		 "users": {
				"GUEST": {"disabled": false, "admin_channels": ["*"] }
		  }
      }
   },
  "persona" : {
     "origin" : "http://example.com/",
     "register" : true
  }
}

After that I tried to add some validation of the documents properties, which results in an error:

13:48:24.307871 CRUD+: Invoking sync on doc “e232a5c1-28ef-4630-b8b5-84819bc20d2d” rev 1-0afc211e2d8b613023792956b71069ea

13:48:24.413340 WARNING: Sync fn exception: SyntaxError: Unexpected token : (line 11); doc = map[_rev:1-0afc211e2d8b613023792956b71069ea title:UsersTestDocumenttitle _id:e232a5c1-28ef-4
630-b8b5-84819bc20d2d created_at:2015-06-10T20:48:21.802Z channels:main_channel writers:[skipxxx] type:UsersTestDocument creator:skipxxx _revisions:map[start:%!s(float64=1) ids:[0afc211
e2d8b613023792956b71069ea]] checked:%!s(bool=false)] -- db.(*Database).getChannelsAndAccess() at crud.go:715

13:48:24.413340     BulkDocs: Doc "e232a5c1-28ef-4630-b8b5-84819bc20d2d" --> 500 Exception in JS sync function (500 Exception in JS sync function)

Here is the current sync gateway configuration:

{
   "log": ["CRUD", "CRUD+", "HTTP", "HTTP+", "Access", "Cache", "Changes", "Changes+"],
   "interface":":4984",
   "adminInterface":":4985",
   "facebook":{
      "register":true
   },
   "databases":{
      "gw":{
         "server":"http://localhost:8091",
         "bucket":"sync_gateway",
         "sync": `function (doc, oldDoc) {
				if (doc._deleted) {
					requireRole("role:editor");
					requireUser(oldDoc.writers);
					return;
				}
				if (!doc.title || !doc.creator || !doc.writers) {
					throw(forbidden: "Missing required properties");
				} else if (doc.writers.length == 0) {
					throw(forbidden: "No writers");
				}
				if (oldDoc == null) {
					requireRole("role:editor");
					requireUser(doc.creator)
				} else {
					requireUser(oldDoc.writers);
					if (doc.creator != oldDoc.creator) {
						throw(forbidden: "Can't change creator");
					}
				}
				//channel(doc.channels);
		 `,
	 "users": {
        	"GUEST": {"disabled": false, "admin_channels": ["*"] }
	  }
      }
   },
  "persona" : {
     "origin" : "http://example.com/",
     "register" : true
  }
}

#2

Hi @skipidar,

throw takes a JS object as parameter {forbidden: "Some message..."}.
See this example https://github.com/couchbaselabs/ToDoLite-Android/blob/master/sync-gateway-config.json#L16.

James


#3

The sync function was correct.
I flushed the bucket before and this removed some required data.
After deleting and recreating the bucket everything was fine again.