Hi everyone.
On a site I'm creating, there's a newsletter form. I'm using Brevo (previously SendinBlue) to add the email entered in the newsletter form to a contact list.
The site is hosted on CloudFlare Pages, so I'm using the functions API to send a POST request to
If I run
However when I send a POST request to a slightly changed
I'm sending the post request from a React component:
This is the error output from my local server:
Let me know if you need any more code/information.
Thanks.
On a site I'm creating, there's a newsletter form. I'm using Brevo (previously SendinBlue) to add the email entered in the newsletter form to a contact list.
The site is hosted on CloudFlare Pages, so I'm using the functions API to send a POST request to
/functions/api/newsletter.js
, and the newsletter.js uses the SendinBlue API to add a contact to a contact list.If I run
node newsletter.js
with this code it seems to work well (of course with an actual API key):
JavaScript:
const SibApiV3Sdk = require("sib-api-v3-sdk");
let defaultClient = SibApiV3Sdk.ApiClient.instance;
let apiKey = defaultClient.authentications["api-key"];
apiKey.apiKey = "API KEY";
let apiInstance = new SibApiV3Sdk.ContactsApi();
let createContact = new SibApiV3Sdk.CreateContact();
createContact.email = "[email protected]";
createContact.listIds = [3];
apiInstance.createContact(createContact).then(function(data) {
console.log("API called successfully. Returned data: " + JSON.stringify(data));
}, function(error) {
console.error(error);
});
However when I send a POST request to a slightly changed
newsletter.js
it doesn't work anymore and is giving a 500 internal server error.
JavaScript:
export const onRequestPost = async ({ request }) => {
const { email } = await request.json();
console.log(email); // outputs the email
const SibApiV3Sdk = require("sib-api-v3-sdk");
let defaultClient = SibApiV3Sdk.ApiClient.instance;
let apiKey = defaultClient.authentications["api-key"];
apiKey.apiKey = "API KEY";
let apiInstance = new SibApiV3Sdk.ContactsApi();
let createContact = new SibApiV3Sdk.CreateContact();
createContact.email = email;
createContact.listIds = [3];
apiInstance.createContact(createContact).then(function(data) {
console.log("API called successfully. Returned data: " + JSON.stringify(data));
}, function(error) {
console.error(error);
});
}
I'm sending the post request from a React component:
JavaScript:
import React, { useEffect } from "react";
import "./Connect.css";
export default function Connect() {
useEffect(() => {
document.getElementById("email-form").onsubmit = async function(e) {
e.preventDefault();
fetch("/api/newsletter", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email: document.getElementById("newsletter-email").value })
})
.then(async res => {
console.log(res)
})
.catch(async err => {
console.error('There was an error!', err);
});
}
});
return(
----- Some HTML -----
<form id="email-form" method="post">
<input placeholder="EMAIL" type="email" id="newsletter-email" className="ne" name="newsletter" required />
<input type="submit" className="newsletter-button" id="newsletterID" value="" />
</form>
----- Some HTML -----
);
}
This is the error output from my local server:
Code:
[mf:err] TypeError: superagent.agent is not a constructor
at new exports2 (C:\Users\Owner\AppData\Documents\react\server\client\node_modules\sib-api-v3-sdk\src\ApiClient.js:99:20)
at null.<anonymous> (C:\Users\Owner\AppData\Documents\react\server\client\node_modules\sib-api-v3-sdk\src\ApiClient.js:598:22)
at null.<anonymous> (C:\Users\Owner\AppData\Documents\react\server\client\node_modules\sib-api-v3-sdk\src\ApiClient.js:24:22)
at ../node_modules/sib-api-v3-sdk/src/ApiClient.js (C:\Users\Owner\AppData\Documents\react\server\client\node_modules\sib-api-v3-sdk\src\ApiClient.js:32:1)
at __require22 (functionsWorker-0.9865949822392073.js:69:50)
at null.<anonymous> (C:\Users\Owner\AppData\Documents\react\server\client\node_modules\sib-api-v3-sdk\src\index.js:23:30)
at ../node_modules/sib-api-v3-sdk/src/index.js (C:\Users\Owner\AppData\Documents\react\server\client\node_modules\sib-api-v3-sdk\src\index.js:25:1)
at __require22 (functionsWorker-0.9865949822392073.js:69:50)
at onRequestPost (C:\Users\Owner\AppData\Documents\react\server\client\functions\api\newsletter.js:33:23)
at async next (C:\Users\Owner\AppData\Documents\react\server\client\node_modules\wrangler\templates\pages-template-worker.ts:161:22)
Let me know if you need any more code/information.
Thanks.