2

I am currently trying to incorporate mailChimp API into my project, but the resFile code that is responsible for sending the success code isn't working.

The code:

    async function run(){
        try {
            const res = await mailchimp.lists.addListMember(listId, {
                email_address: subscribingUser.emailAddress,
                status: "subscribed",
                merge_fields: {
                    NAME: subscribingUser.userName
                }
            });
            console.log(`Successfully added contact as an audience member. The contact's id is ${res.id}.`);
            console.log(res);
            //this res.sendFile does not send the file
            res.sendFile(__dirname + "/success.html");
        } catch(error) {
            //Code went straight to this and answered "error undefined"
            console.log(`Error ${error.status}.`);
            //This file was successfully sent following the "error undefined"
            res.sendFile(__dirname + "/failure.html");
        }
    }
    run();
});

I tried:

1)Renaming the success file (doesn't work)

2)Swapping the success and failure file (doesn't work)

All gave the same outcome Expected outcome: Sending the success file to the client upon successful completion. subscribingUser is successfully created Actual outcome: Sending the failure file to client despite the successful completion, terminal says "error undefined". subscribingUser is successfully created.

Edit 1(8 hours after this post was made): I solved the problem by changing the

const res = await mailchimp.....

to

const response = await mailchimp...

The whole working code snippet is pasted below. However, I don't yet know why it worked. I have some theories in the code snippet below. If someone is able to shed light on why, I would be thankful if you could do so. Thank you for all those that helped!

///This is the res I am trying to call.
app.post("/", function(req,res){
    //The client's name and email
    const clientName = req.body.userName;
    const clientEmail = req.body.userEmail;
    //my unique list ID is here, so I censored it.
    const listId = "XXXXXXX";
    const subscribingUser = {
        emailAddress: clientEmail,
        userName: clientName
    }
    //Showing us the values of the variables in the object subscribingUser
    //Tells us what the user had inputted into the website.
    console.log(subscribingUser);
    async function run(){
        try {
            ///This was probably why it registered the res.SendFIle in the try 
            ///function as a function, as it overrode the res mentioned above at
            ///app.post.
            const response = await mailchimp.lists.addListMember(listId, {
                email_address: subscribingUser.emailAddress,
                status: "subscribed",
                merge_fields: {
                    NAME: subscribingUser.userName
                }
            });
            console.log(`Successfully added contact as an audience member. The contact's id is ${response.id}.`);
            res.sendFile(__dirname + "/success.html");
        } catch(error) {
            console.log(`Error ${error}.`);
            res.sendFile(__dirname + "/failure.html");
        }
    }
    run();
});
  • 2
    Do `console.log(error)` error and see what is shows – Amir Saleem Apr 03 '21 at 05:07
  • 1
    Your code log `error.status` not the actual `error`. – Anh Nhat Tran Apr 03 '21 at 05:10
  • For starters, you can install an error handler function and see if there's an error `res.sendFile(someFile, function(err) { if (err) console.log(err); })`. – jfriend00 Apr 03 '21 at 05:38
  • Looks like your code is failing before res.sendFile. Are you getting the console.log message which you displayed as "Successfully added ....."? – Amaranadh Meda Apr 03 '21 at 09:14
  • Add the folder structure to the post. – hbamithkumara Apr 03 '21 at 09:28
  • @AmirSaleem @AnhNhatTran @AmaranadhMeda Changed the error.status to error for "console.log(`Error ${error}`); The terminal said "typeError: resFile is not a function" An excerpt of the console message is this: [link]https://gyazo.com/37790a822ad20fe4f05ed231f1d522f2 @hbamithkumara This is the folder structure [link]https://gyazo.com/0e42e608d86428dad28fa1657df57e9a – Scarypotatos Apr 03 '21 at 12:45

1 Answers1

0

Try this:

res.sendFile('success.html', {root: __dirname })

Alberto Camino
  • 329
  • 2
  • 11