You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Brevo NodeJS批量事务邮件报错:Cannot read properties of undefined (reading 'instance')

TypeError: Cannot read properties of undefined (reading 'instance') 解决方案

问题场景

基于Node.js v20.10.0开发Brevo批量自定义事务邮件发送功能,严格按照官方文档编写代码后,出现如下错误:

let defaultClient = brevo.ApiClient.instance;
^

TypeError: Cannot read properties of undefined (reading 'instance')
at Object. (C:\Users\HARIHARAN REDDY\Desktop\New folder\index.js:2:37)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49

原代码如下:

const brevo = require("@getbrevo/brevo");
let defaultClient = brevo.ApiClient.instance;

let apiKey = defaultClient.authentications["apiKey"];
apiKey.apiKey = "xkeysib-xx-xxx-xxx-xxxxx-xxx";

let apiInstance = new brevo.TransactionalEmailsApi();
let sendSmtpEmail = new brevo.SendSmtpEmail();

sendSmtpEmail.subject = "My {{params.subject}}";
sendSmtpEmail.htmlContent =
  "<html><body><h1>Common: This is my first transactional email {{params.parameter}}</h1></body></html>";
sendSmtpEmail.sender = { name: "John", email: "abc@gmail.com" };
sendSmtpEmail.to = [{ email: "brevo@brevo.com", name: "John" }];
sendSmtpEmail.replyTo = { email: "brevo@brevo.com", name: "John" };
sendSmtpEmail.headers = { "Some-Custom-Name": "unique-id-1234" };
sendSmtpEmail.params = {
  parameter: "My param value",
  subject: "common subject",
};
sendSmtpEmail.messageVersions = [
  {
    to: [
      {
        email: "xyz@gmail.com",
        name: "John",
      },
    ],
    headers: {
      "Message-Id": "<123.123@smtp-relay.mailin.fr>",
    },
    params: {
      greeting: "Welcome onboard!",
      headline: "Be Ready for Takeoff.",
    },
    subject: "+001",
    htmlContent: "<html><body><h1>+001 content</h1></body></html>",
  },
  {
    to: [
      {
        email: "def@gmail.com",
        name: "Steve",
      },
    ],
    params: {
      greeting: "Greeting 1.",
      headline: "Some bathing suits you might like",
    },
    subject: "+002",
  },
];

apiInstance.sendTransacEmail(sendSmtpEmail).then(
  function (data) {
    console.log(
      "API called successfully. Returned data: " + JSON.stringify(data)
    );
  },
  function (error) {
    console.error(error);
  }
); 

问题原因

错误源于@getbrevo/brevo SDK版本差异:当前使用的是v2.x+版本,而官方文档可能未及时更新,仍保留了v1版本的ApiClient.instance初始化写法,新版本已重构了客户端初始化逻辑。

解决步骤

  1. 确保安装最新版SDK:
npm install @getbrevo/brevo@latest
  1. 修改代码中的客户端初始化部分,替换原有的前4行代码,完整修改后的代码如下:
const { TransactionalEmailsApi, SendSmtpEmail, Configuration } = require('@getbrevo/brevo');

const configuration = new Configuration({
  apiKey: "xkeysib-xx-xxx-xxx-xxxxx-xxx"
});

let apiInstance = new TransactionalEmailsApi(configuration);
let sendSmtpEmail = new SendSmtpEmail();

// 以下保留原代码的邮件配置逻辑不变
sendSmtpEmail.subject = "My {{params.subject}}";
sendSmtpEmail.htmlContent =
  "<html><body><h1>Common: This is my first transactional email {{params.parameter}}</h1></body></html>";
sendSmtpEmail.sender = { name: "John", email: "abc@gmail.com" };
sendSmtpEmail.to = [{ email: "brevo@brevo.com", name: "John" }];
sendSmtpEmail.replyTo = { email: "brevo@brevo.com", name: "John" };
sendSmtpEmail.headers = { "Some-Custom-Name": "unique-id-1234" };
sendSmtpEmail.params = {
  parameter: "My param value",
  subject: "common subject",
};
sendSmtpEmail.messageVersions = [
  {
    to: [
      {
        email: "xyz@gmail.com",
        name: "John",
      },
    ],
    headers: {
      "Message-Id": "<123.123@smtp-relay.mailin.fr>",
    },
    params: {
      greeting: "Welcome onboard!",
      headline: "Be Ready for Takeoff.",
    },
    subject: "+001",
    htmlContent: "<html><body><h1>+001 content</h1></body></html>",
  },
  {
    to: [
      {
        email: "def@gmail.com",
        name: "Steve",
      },
    ],
    params: {
      greeting: "Greeting 1.",
      headline: "Some bathing suits you might like",
    },
    subject: "+002",
  },
];

apiInstance.sendTransacEmail(sendSmtpEmail).then(
  function (data) {
    console.log(
      "API called successfully. Returned data: " + JSON.stringify(data)
    );
  },
  function (error) {
    console.error(error);
  }
); 

关键修改说明

  • 改用解构导入,直接获取所需的TransactionalEmailsApi、SendSmtpEmail和Configuration类
  • 通过Configuration实例配置API密钥,替代旧版的ApiClient.instance认证方式
  • 初始化TransactionalEmailsApi时传入配置实例,确保客户端正确初始化

内容的提问来源于stack exchange,提问作者S . Hariharan Reddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:35:21