Can I use variables in Sync Function?

My function

function (doc, oldDoc) {
        if (Array.isArray(doc.owner)){
                var limit = doc.owner.length;
                for(i = 0; i < limit; i++){
                        if (doc.owner[i].indexOf("_") > - 1 && !doc.owner.includes(doc.owner[i].substring(0,doc.owner[i].indexOf("_")).toLowerCase()))
                                doc.owner.push(doc.owner[i].substring(0,doc.owner[i].indexOf("_")).toLowerCase());
                }
        } else {
                if (doc.owner.indexOf("_") > - 1) {
                        channel([doc.owner.substring(0,doc.owner.indexOf("_")).toLowerCase(), doc.owner]);
                } else {
                        channel(doc.owner);
                }
        }
}

trows the following error while executing and first if condition is satisfied:

WARNING: Sync fn exception: TypeError: undefined is not a function;

May be is not allowed to use variables in the sync function body?

@paolo.morgano

Yes variables are allowed, see the todo-lite sync function

1 Like

Thanks a lot. Is there a way to have console output to add some debug info to my function?

yes, you can use console.log("log some thing"); even console.debug("log some thing");

1 Like

Thank all! I’ve figured out where my function was failing:

  1. use of doc.owner.includes is not supported by sync gateway javascript interpreter
  2. also the check doc.owner.indexOf("_") was throwing error when doc had no owner property

This is my final working function:

function (doc, oldDoc) {
  if (doc.owner !== undefined && doc.owner !== null) {
    // console.log("Task sync on doc.owner: " + doc.owner );
    if (Array.isArray(doc.owner)){
      var limit = doc.owner.length;
      var channels = [];
        // console.log("Limit is " + limit);
      for(var i = 0; i < limit; i++){
        // console.log("Testing element " + i + " value " + doc.owner[i]);
        channels.push(doc.owner[i]);
        var constainsTenant = (doc.owner[i].indexOf("_") > - 1);
          // console.log("Contains tenanat? " + constainsTenant);
        if (constainsTenant) {
          var tenant = doc.owner[i].substring(0,doc.owner[i].indexOf("_")).toLowerCase();
          // console.log("Tenant: " + tenant);
          var indexOfTenant = doc.owner.indexOf(tenant);
            // console.log("Tenant index: " + indexOfTenant);
          var tenantIsOwner = indexOfTenant == -1? false:true;
            // console.log("Tenant is owner? " + tenantIsOwner);
          if (!tenantIsOwner){
            // console.log("Pushing tentant " + tenant);
            channels.push(tenant);
            // console.log("Push tentant ");
            // console.log("owners: " + doc.owner);
          }
        }
      }
        // console.log("Add channels doc.owner: " + doc.owner );
      channel(channels);
    } else {
      if (doc.owner.indexOf("_") > - 1) {
        channel([doc.owner.substring(0,doc.owner.indexOf("_")).toLowerCase(), doc.owner]);
      } else {
        channel(doc.owner);
      }
    }
  } else {
    console.log("Tour sync skipped");
  }
}

yes, indexOf() and JSON.stringify() wanted too! @andy @adamf
see also

@atom_yang I think those should all be supported - Sync Gateway is pinned to an otto commit that’s later than the commit you’ve linked to.

It looks like @paolo.morgano is using indexOf successfully above. I haven’t tried JSON.stringify, but based on that otto commit it should also work.

I have tried JSON.stringify ,and it return the following error

WARNING: Sync fn exception: ReferenceError: JSON is not defined;

@adamf indexOf works, but obviously throws an error if doc.owner is undefined in my usage.

JSON.stringify, instead, wasn’t working.

I’m working on Sync Gateway 1.2.

P

You can’t use let or const either which is frustrating. Are there any plans to use a more recent version of JS?

@paolo.morgano Apologies - it looks like JSON.stringify didn’t make it to otto until after our pinned commit. I’ve filed a ticket to move to a more recent otto to pick up that support. https://github.com/couchbase/sync_gateway/issues/2471

@nick-couchbase I took a quick look for go javascript interpreters that support ECMA6, but didn’t find anything. I think it may be on otto’s roadmap (let and const are there as ‘future keywords’), but it’s not available yet.

1 Like