【问题标题】:Error importing SendInBlue in my NodeJS app?在我的 NodeJS 应用程序中导入 SendInBlue 时出错?
【发布时间】:2023-01-04 07:44:40
【问题描述】:

我正在重构我的 Node 应用程序以使用 ES6 导入模块而不是在文件中要求。

我无法弄清楚如何使用我的电子邮件包 SendInBlue 进行这项工作,因为我收到了错误。

类型错误:无法读取未定义的属性(读取“ApiClient”)

在我使用“sib-api-v3-sdk”中的“import {SibApiV3Sdk}”之前,我收到了错误:

SyntaxError: Named export 'SibApiV3Sdk' not found. The requested module 'sib-api-v3-sdk' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'sib-api-v3-sdk';
const { SibApiV3Sdk } = pkg;

^ 所以这就是为什么我使用下面的当前导入代码,它也不起作用?

我怎样才能让它工作?

代码

import pkg from "sib-api-v3-sdk"
const { SibApiV3Sdk } = pkg"
const defaultClient = SibApiV3Sdk.ApiClient.instance
const apiKey = defaultClient.authentications["api-key"]
apiKey.apiKey = process.env.SEND_IN_BLUE_API_KEY
import secretCodeHtml from "../templates/secretCodeEmail.js"

const sendSecretCodeEmail = (text) => {
  var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
  var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail()
  sendSmtpEmail = {
    sender: { email: senderEmail },
    to: [
      {
        email: recipientEmail,
        name: recipientEmail,
      },
    ],
    subject: emailSubject,
    htmlContent: text,
  }
  apiInstance.sendTransacEmail(sendSmtpEmail)
}

export default sendSecretCodeEmail

错误

file:///Users/app/git/app-node-api/src/emails/create/sendSecretCodeEmail.js:4
const defaultClient = SibApiV3Sdk.ApiClient.instance
                                  ^
TypeError: Cannot read properties of undefined (reading 'ApiClient')
    at file:///Users/app/git/app-node-api/src/emails/create/sendSecretCodeEmail.js:4:35
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

【问题讨论】:

    标签: javascript node.js sendinblue


    【解决方案1】:

    在这种情况下,像这样在导入中使用别名:

    import * as sib from "sib-api-v3-sdk";
    

    【讨论】:

    • 您的答案可以通过其他支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写出好的答案的信息in the help center
    【解决方案2】:

    改用这个,

    const pkg =  require('sib-api-v3-sdk');
    

    如果您遇到类型挑战,请在项目的根目录下创建一个文件,您可以将其命名为 index.d.ts,添加,

    declare module 'sib-api-v3-sdk'
    

    进入空文件并保存。

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 2022-07-30
      • 2012-02-20
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多