0

I am exploring how to download document from Amazon S3 and then save it as an attachment on a Xero Invoice. Can I have help on this task? I am referencing my code using these two links:

Error: I keep receiving this error while sending it to Xero.

TypeError: body.on is not a function

Main

const sendInvoicesResponse = await xeroService.sendDocument(
      mappedDocumentData,
      integration.tenant.id,
    );
const downloadedDocument = await awsService.downloadDocument(document.key);
await xeroService.sendAttachmentToInvoice(
      integration.tenant.id,
      sendInvoicesResponse.body.invoices[0].invoiceID,
      document.name,
      downloadedDocument.data,
      downloadedDocument.mimetype,
    );

AWS Service

module.exports.downloadDocument = async (key) => {
  try {
    const getParams = {
      Bucket: config.aws.documentsBucket, // your bucket name,
      Key: key,
    };
    const file = await s3.getObject(getParams).promise();

    return {
      data: file.Body,
      mimetype: file.ContentType,
    };
  } catch (error) {
    logger.error(`download document from AWS fail | message=${message}`);
    throw new Error(message);
  }
};

In Xero

module.exports.sendAttachmentToInvoice = async (
  tenantId,
  invoiceId,
  fileName,
  body,
  contentType,
  includeOnline = true,
) => {
  try {
    return client.accountingApi.createInvoiceAttachmentByFileName(
      tenantId,
      invoiceId,
      fileName,
      body,
      includeOnline,
      {
        headers: {
          'Content-Type': contentType,
        },
      },
    );
  } catch (error) {
    logger.error(`attaching file attachment to xero fail | message=${message}`);
    throw new Error(message);
  }
};
smac2020
  • 9,637
  • 4
  • 24
  • 38
siri
  • 61
  • 3
  • Presumably you wrote some code, which is generating that error? Please include your code in the Question. – John Rotenstein Jul 13 '21 at 08:32
  • Instead of referencing some third party JS AWS examples, you can find the official examples in the AWS Example GITHUB repo here: https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code – smac2020 Jul 13 '21 at 13:29

0 Answers0