aws javascript sdk registerThing 模板正文不接受有效的 json 字符串

aws javascript sdk registerThing template body does not accept valid json string

我正在将模板 json 传递给 registerThing 函数,如下面的代码部分所示。

const templateJson = {"Parameters":{"Attr1":{"Type": "String"},"Attr2": {"Type": "String"},"AWS::IoT::Certificate::Id":{"Type": "String"}},"Resources":{"certificate":{"Properties": {"CertificateId":{"Ref": "AWS::IoT::Certificate::Id"},"Status": "Active"},"Type": "AWS::IoT::Certificate"},"policy":{"Properties": {"PolicyDocument": "<<Inline olicy document>>"},"Type": "AWS::IoT::Policy"},"thing": {"OverrideSettings":{"AttributePayload": "MERGE","ThingGroups": "DO_NOTHING","ThingTypeName": "REPLACE"},"Properties":{"AttributePayload":{},"ThingGroups":["mygroup"],"ThingName": {"Ref": "SerialNumber"},"ThingTypeName": "testname"},"Type": "AWS::IoT::Thing"}},"DeviceConfiguration":{}};

const provisionTemplateBody = JSON.stringify(templateJson);

然后用这个构造函数参数为

 var paramsRegisterThing = {
        templateBody: provisionTemplateBody,
        parameters: provisioningTemplateParams
    };

用这个调用 registerThing 时,出现如下异常

"errorType": "InvalidRequestException",
  "errorMessage": "Invalid registration template. Template format error: unsupported type or structure...

我已经按照 Whosebug 上其他一些问题中的建议添加了 JSON.stringify,但这没有帮助。

我怎样才能完成这项工作? 或者有没有其他方法可以通过模板名称或 ARN 而不是主体来注册东西?

我注意到的几件事:

  • 删除 DeviceConfiguration
  • 的空块
  • 参数块中缺少对 SerialNumber 的引用。
  • 您在参数中还有另外两个属性 Attr1 和 Attr2,它们实际上并未在资源块中使用。

如果模板和参数都是两个Javascript对象。 templateBody 应该是一个字符串,参数应该是键值映射。

 iot.registerThing(
      {
        templateBody: JSON.stringify(template1),
        parameters: parms,
      },
      function (err, data) {}
 )