Jon Nicholson
Jon Nicholson

Reputation: 1131

Why is my app.js not detecting errors with my Mailchimp API?

I've built a newsletter API with Mailchimp, and new users can successfully sign up to my newsletter by filling in the form and submitting. However, if there's an error in signing the user up (for example, if they've already signed up), then my current code does not detect this, and still re-directs the user to a 'sign up success' page.

The error however is logged in my terminal - for example, if the contact exists, this error is logged:

error_code: 'ERROR_CONTACT_EXISTS'

I'm currently using a really simple else-if statement that re-directs the user to the success page if the status code === 200.

  const request = https.request(url, options, function(response) {

    if (response.statusCode === 200) {
      res.sendFile(__dirname + "/newsletter-success.html");
    } else {
      res.sendFile(__dirname + "/newsletter-failure.html");
    }

    response.on("data", function(data) {
      console.log(JSON.parse(data));
    });
  });

I've tried adapting the callback to pass in errors but not sure if that's right.

Is there a better way of re-structuring my code so that it redirects if successful, and if there's an error then it'll take the user to the failure page?

Thanks.

Upvotes: 0

Views: 178

Answers (2)

Abderrahmane Rhiabi
Abderrahmane Rhiabi

Reputation: 13

hi friend i hope you doing well

i am junior developer and i think i have a solution to the problem you mentioned with your newsletter API.

take a look the solution that i maked :

 app.post("/", (req,res) => {

    const firstName = req.body.firstName;
    const lastName = req.body.lastName;
    const email = req.body.email;
    console.log(firstName, lastName, email);

    const data = {
        members : [
            {
                email_address : email,
                status : "subscribed",
                merge_fields : {
                    FNAME : firstName,
                    LNAME : lastName
                }
            }
        ]
    }

    //to send it it the server (mailchamp)
    const post_jsonData = JSON.stringify(data);

    const url = "https://us**.api.mailchimp.com/3.0/lists/**";


    //set up the options
    const options = {
        method : "POST",
        auth : "**:**"
    };



    //set up the request
    const post_request = https.request(url, options, (response)=>{
        response.on("data", (chunk) => {
            console.log(JSON.parse(chunk));
            const jsonChunk = JSON.parse(chunk);
            console.log("================");
            
            if(response.statusCode === 200){
                const error_code = jsonChunk.errors[0];
                console.log(error_code);

                if (!error_code) {

                    res.sendFile(__dirname + "/success.html");
               }
               else {
                    res.sendFile(__dirname + "/failure.html");
               }
            } else{
                res.send("Key problem");
            }
        })

    })

    post_request.write(post_jsonData);
    post_request.end();

})

The part the you need : Solution

and if you want the solution of newsletter challenge i put it in my github you can check it if you want : https://github.com/Abderrahmane01Rhiabi/Newsletter-challenge

Best regards,

Upvotes: 0

Ayan
Ayan

Reputation: 551

This error basically means that the user already exists on their mailing list of Mailchimp and cannot be further added. You can check them from the console of mailchimp or just logging the request sent by their servers to your terminal

Upvotes: 0

Related Questions