xml2js 在 NodeJS 的 Lambda 函数中崩溃

问题描述 投票:0回答:1

我正在尝试通过 Lambda 函数在 NodeJS 中实现此 XML 输出:

<Section ProductId="46m8t">
        <Item ItemQty="1">
        <Image>Road.jpg</Image>
        <ItemProductValue>0.50</ItemProductValue>
        <ItemShippingValue>1.00</ItemShippingValue>
      </Item>
</Section>

从此:

Sections: listedObjects.Contents.map(file => ({
      Section: {
        $: { ProductId: "46m8t" },
        Item: {
          $: {
            ItemQty: "1",
            Image: file.Key.split('/').pop(),
            ItemProductValue: "0.50",
            ItemShippingValue: "1.00"
          }
        }
      }
    }))

使用此构建器设置:

    const builder = new xml2js.Builder({
       headless: true,
       renderOpts: { 'pretty': true },
       explicitArray: true,
       explicitChildren: true,
       preserveChildrenOrder: true
    });

当 XML 构建失败时,该函数就会消失。

这有效:

    Sections: listedObjects.Contents.map((file) => ({
          Section: {
              $: { ProductId: "46m8t" }, // Included ProductId directly in the Section tag
              Item: {
                  ItemQty: "1",
                  Image: file.Key.split('/').pop(), // Only the filename
                  ItemProductValue: "0.50",
                  ItemShippingValue: "1.00"
              }
          }
    }))

但给了我这个输出:

   <Sections>
     <Section ProductId="46m8t">
         <Item>
           <ItemQty>1</ItemQty>
            <Image>Road.jpg</Image>
            <ItemProductValue>0.50</ItemProductValue>
            <ItemShippingValue>1.00</ItemShippingValue>
          </Item>
        </Section>
     </Sections>

使用此生成器:

     const builder = new xml2js.Builder({ headless: true, renderOpts: { 'pretty': true } });
node.js aws-lambda xml2js
1个回答
0
投票

事实证明这个语法成功了:

Sections: {
    Section: listedObjects.Contents.map(file => ({
      $: { ProductId: "46m8t" },
      Item: {
        $: {
          ItemQty: "1"
        },
        Image: file.Key.split('/').pop(),
        ItemProductValue: "0.50",
        ItemShippingValue: "1.00"
      }
    }))
  }
}

};

© www.soinside.com 2019 - 2024. All rights reserved.