Out-of-date sync gateway view example in the docs?

#1

I was using this guide: http://developer.couchbase.com/mobile/develop/guides/sync-gateway/accessing-cb-views/creating-and-using-a-view/index.html, and following the testview example… but I wasn’t getting any results. On inspection via the admin ui I noticed the view is actually:

function(doc,meta) {
                    var sync = doc._sync;
                    if (sync === undefined || meta.id.substring(0,6) == "_sync:")
                      return;
                    if ((sync.flags & 1) || sync.deleted)
                      return;
                    delete doc._sync;
                    meta.rev = sync.rev;
					(function (doc, meta) { if (doc._sync === undefined || meta.id.substring(0,6) == "_sync:") { return; } if (doc.type != "list") { return; } emit(doc.title, doc.owner); }) (doc, meta); }

You can see the sync stuff is in twice.

Looking on these forums, I noticed the extra stuff from the link above:

if (doc._sync === undefined || meta.id.substring(0,6) == \"_sync:\") { return; }

isn’t in peoples view snippets, ie:

{
"views":{
"all_lists":{
"map":"function (doc, meta) { if (doc.type != \"list\") { return; } emit(doc.title, doc.owner); }"
}
}
}

Getting rid of it means everything now works… Are the official docs out of date?

#2

Correct me if I’m wrong but the JS function below is what’s in the docs page you referenced in your post.

function (doc, meta) {
    if (doc._sync === undefined || meta.id.substring(0,6) == \"_sync:\") {
        return;
    } if (doc.type != \"list\") {
        return;
    }
    emit(doc.title, doc.owner);
}

It’s ignoring docs with an id that starts with _sync (because they are for internal use only by Sync Gateway so can safely be ignored) and docs of type list.

Let us know what’s not working with this function. I’m not sure I understand what’s going on.

Cheers
James

#3

Yes you are correct, that is what’s written in the docs page I referenced. What I’m saying is when you actually follow the steps in the docs, and add that exact view via the rest api… It becomes when viewed in the admin ui:

function(doc,meta) {
                var sync = doc._sync;
                if (sync === undefined || meta.id.substring(0,6) == "_sync:")
                  return;
                if ((sync.flags & 1) || sync.deleted)
                  return;
                delete doc._sync;
                meta.rev = sync.rev;
				(function (doc, meta) { if (doc._sync === undefined || meta.id.substring(0,6) == "_sync:") { return; } if (doc.type != "list") { return; } emit(doc.title, doc.owner); }) (doc, meta); }

So it looks to me like duplication. 3rd line, and last line…

meta.id.substring(0,6) == "_sync:"

and the structure looks a little odd.

I noticed in other posts, one of yours for example, you don’t have any references to _sync in your defined view that you are adding via the REST api, where as the doc version does.

{
  "views": {
    "by_creator": {
      "map": "function(doc, meta) {emit(doc.creator_id);}",
      "reduce": "_count"
     }
  }
}

So when I follow suit, drop all the _sync related stuff from my custom view, they work… as it appears to me SG is adding this stuff automatically.