0

I had a strange encounter when I was trying to update user data with Google App Script via the Admin SDK Directory Service I wanted to add some values for a customschema like this:

 var myCustomschema = {};

  myCustomschema.someField = "foo";
  myCustomschema.anotherField = "bar";

  var resource = {};
  resource.customSchemas = myCustomschema;
  var resp = AdminDirectory.Users.patch(resource, user@mydomain.de);

I did not get any error back however the data was never changed in the admin directory. I also tried to JSON.stringify(resource).

Then I tried something like this:

var test = {
"customSchemas": {
"myCustomschema": {
  "someField":  "foo",
  "anotherField": "bar"
}
}
 };

And this worked!

So I was wondering why? Is there even a difference between the two approached in the Javascript world? Apparently there is a difference in the Google App Script world.

zlZimon
  • 2,334
  • 4
  • 21
  • 51
  • 1
    `resource.customSchemas = myCustomschema` should be `resource.customSchemas = { myCustomschema: myCustomschema };` Otherwise, you're missing a step – blex Jun 26 '20 at 12:47

1 Answers1

4

You're missing out the myCustomschema level in your first example.

Your first is:

{
    "customSchemas": {
        "someField": "foo",
        "anotherField": "bar"
    }
}

Your second is:

{
    "customSchemas": {
        "myCustomschema": {
            "someField": "foo",
            "anotherField": "bar"
        }
    }
}

Compare:

var myCustomschema = {};

myCustomschema.someField = "foo";
myCustomschema.anotherField = "bar";

var resource = {};
resource.customSchemas = myCustomschema;
console.log(JSON.stringify(resource, null, 4));

console.log("vs");

var test = {
      "customSchemas": {
          "myCustomschema": {
              "someField": "foo",
              "anotherField": "bar"
        }
    }
};

console.log(JSON.stringify(test, null, 4));
.as-console-wrapper {
    max-height: 100% !important;
}

You can fix it with shorthand property notation:

resource.customSchemas = { myCustomschema };

var myCustomschema = {};

myCustomschema.someField = "foo";
myCustomschema.anotherField = "bar";

var resource = {};
resource.customSchemas = { myCustomschema };
console.log(JSON.stringify(resource, null, 4));

console.log("vs");

var test = {
      "customSchemas": {
          "myCustomschema": {
              "someField": "foo",
              "anotherField": "bar"
        }
    }
};

console.log(JSON.stringify(test, null, 4));
.as-console-wrapper {
    max-height: 100% !important;
}

Or of course the old way:

resource.customSchemas = { myCustomschema: myCustomschema };
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875