sendinblue / APIv3-nodejs-library

SendinBlue's API v3 Node.js Library
ISC License
101 stars 47 forks source link

Sendinblue create contact 'TypeError: Failed to fetch' error (using React) #82

Closed 1cookspe closed 2 years ago

1cookspe commented 4 years ago

I am trying to implement the Sendinblue API in my React JS application. I have a text input that the user is to enter their email into, and then a button that will add their email to a Sendinblue list. I am using the "Create a contact" reference located here in Node.js, as I am using the JavaScript code for my React app. Sometimes, this code works, as the email is successfully added to the SendinBlue list, however most times the email is not added and I receive the following error:

var request = require("request");

My full code is seen below:

var options = {
  method: 'POST',
  url: 'https://api.sendinblue.com/v3/contacts',
  headers: {
    accept: 'application/json',
    'content-type': 'application/json',
    'api-key': 'My SendinBlue Application API Key'
  },
  body: {updateEnabled: false, email: 'newEmail@exampleWebsite.com'},
  json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

When I test this on the Sendinblue API docs, it successfully adds the email to my contact list every time. I test it with a new email each time to ensure that it does not throw an error because of a duplicate email. I have also tinkered with the value of updateEnabled between true and false however it does not seem to help solve this issue.

I believe that the error may stem from the var request = require("request"); command in the first line, as I am not sure if require is a React method or a Node.js method.

If anyone has any ideas on how I could fix this, your help would be greatly appreciated. Thank you.

shubhamUpadhyayInBlue commented 3 years ago

Hi @1cookspe

Regarding this error:

TypeError: Failed to fetch Have a look at this thread, it might help.

Happy to help!

shubhamUpadhyayInBlue commented 2 years ago

Closing this thread since there is no activity on this thread for more than 15 days.