Node.js -> TypeError:brevo.ApiClient 不是构造函数

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

我正在尝试创建一个自动电子邮件程序,在发送姓名和电子邮件等详细信息时,会向给定的电子邮件地址发送一封电子邮件,并注明“已订阅”。这对我来说是一项任务,但我已经被困在这里大约 7 个小时,并尝试了各个方面。

app.post('/submit', (req, res) => {
    const { name, email, subject , sender , to} = req.body;

    
    let defaultClient = new brevo.ApiClient();
    let apiKey = defaultClient.authentications['api-key'];
    apiKey.apiKey = 'API_KEY';
let apiInstance = new Brevo. TransactionalEmailsApi();
let sendSmtpEmail = new Brevo. SendSmtpEmail();
    sendSmtpEmail.to = [{ email: email, name: name }];
    sendSmtpEmail.templateId = 59;
    sendSmtpEmail.params = { name: name }; 
    sentSmtpEmail.subject = {subject:subject};
    sendSmtpEmail.sender = {sender:sender};
    sendSmtpEmail.to = {to:to};

    
    apiInstance.sendTransacEmail(sendSmtpEmail)
        .then(function (data) {
            console.log('Email sent successfully.');
            res.send('Email sent successfully.');
        })
        .catch(function (error) {
            console.error('Error sending email:', error);
            res.status(500).send('Error sending email.');
        });
});

我使用 Brevo API 创建自动电子邮件程序并从其文档中获取了此代码。

const brevo = require('@getbrevo/brevo');
var defaultClient = brevo.ApiClient.instance;

let apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = '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';

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": "[email protected]" };
sendSmtpEmail.to = [
  { "email": "[email protected]", "name": "sample-name" }
];
sendSmtpEmail.replyTo = { "email": "[email protected]", "name": "sample-name" };
sendSmtpEmail.headers = { "Some-Custom-Name": "unique-id-1234" };
sendSmtpEmail.params = { "parameter": "My param value", "subject": "common subject" };


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

以上述格式编写此代码后,我收到如下错误

TypeError: brevo.ApiClient is not a constructor
    at E:\Projects\email-app1\server.js:25:25
    at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\Projects\email-app1\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (E:\Projects\email-app1\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
    at E:\Projects\email-app1\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (E:\Projects\email-app1\node_modules\express\lib\router\index.js:346:12)
    at next (E:\Projects\email-app1\node_modules\express\lib\router\index.js:280:10)
    at E:\Projects\email-app1\node_modules\body-parser\lib\read.js:137:5
    at AsyncResource.runInAsyncScope (node:async_hooks:206:9)

换线后

let defaultClient = new Brevo. ApiClient()

 let defualtClient = brevo.ApiClient.instance
我收到这个错误

TypeError: Cannot read properties of undefined (reading 'instance')
    at E:\Projects\email-app1\server.js:25:41
    at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\Projects\email-app1\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (E:\Projects\email-app1\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (E:\Projects\email-app1\node_modules\express\lib\router\layer.js:95:5)
    at E:\Projects\email-app1\node_modules\express\lib\router\index.js:284:15
    at Function.process_params (E:\Projects\email-app1\node_modules\express\lib\router\index.js:346:12)
    at next (E:\Projects\email-app1\node_modules\express\lib\router\index.js:280:10)
    at E:\Projects\email-app1\node_modules\body-parser\lib\read.js:137:5
    at AsyncResource.runInAsyncScope (node:async_hooks:206:9)

javascript node.js npm backend nodemailer
1个回答
0
投票

我也尝试使用@getbrevo/brevo

我继续搜索文档,这似乎有效

var SibApiV3Sdk = require('sib-api-v3-sdk');
var defaultClient = SibApiV3Sdk.ApiClient.instance;

// Configure API key authorization: api-key
var apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = 'YOUR_API_KEY';

// Uncomment below two lines to configure authorization using: partner-key
// var partnerKey = defaultClient.authentications['partner-key'];
// partnerKey.apiKey = 'YOUR API KEY';

var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();

var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail(); // SendSmtpEmail | Values to send a transactional email

sendSmtpEmail = {
    to: [{
        email: '[email protected]',
        name: 'John Doe'
    }],
    templateId: 59,
    params: {
        name: 'John',
        surname: 'Doe'
    },
    headers: {
        'X-Mailin-custom': 'custom_header_1:custom_value_1|custom_header_2:custom_value_2'
    }
};

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

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