【问题标题】:Are there different Javascript objects in Google App Script?Google App Script 中是否有不同的 Javascript 对象?
【发布时间】:2020-10-17 01:39:20
【问题描述】:

当我尝试通过Admin SDK Directory Service 使用 Google 应用脚本更新用户数据时,我遇到了一个奇怪的情况 我想为这样的 customschema 添加一些值:

 var myCustomschema = {};

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

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

我没有收到任何错误,但是管理目录中的数据从未更改。我也试过JSON.stringify(resource)

然后我尝试了这样的事情:

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

这成功了!

所以我想知道为什么?在 Javascript 世界中接近的两者之间甚至有区别吗?显然,Google App Script 世界有所不同。

【问题讨论】:

  • resource.customSchemas = myCustomschema 应该是 resource.customSchemas = { myCustomschema: myCustomschema }; 否则,你错过了一步

标签: javascript google-apps-script google-admin-sdk


【解决方案1】:

您在第一个示例中错过了myCustomschema 级别。

你的第一个是:

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

你的第二个是:

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

比较:

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;
}

您可以使用简写的属性表示法来修复它:

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;
}

当然还是老办法:

resource.customSchemas = { myCustomschema: myCustomschema };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2021-09-05
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多