Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS 'TypeError: superagent.agent is not a constructor' when sending POST request to functions on CloudFlare Pages

Johna

Frontend Developer
Staff Team
Guardian
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 /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.
 
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 /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.
Are you using the correct http headers?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom