Reputation: 21
How can I fix the below issue? I am unable to submit the form as apparently the listener should be a function. I am using Node Version 14.1.0.
Error:
TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
Relevant Code:
app.post("/about", function(req, res){
const email = req.body.email;
const data = {
members: [
{
email_address: email,
status: "subscribed"
}
]
};
const jsonData = JSON.stringify(data);
const url = process.env.CUST_KEY;
const options = {
method: "POST",
auth: process.env.API_KEY
}
const request = https.request(url, options, function(response) {
if (response.statusCode === 200) {
res.render("success");
} else {
res.render("failure");
}
response.on("data", function(data){
})
})
request.write(jsonData);
request.end();
});
Upvotes: 2
Views: 4611
Reputation: 11
This is my solution, which adds the url within the options and shifts some other stuff around:
app.post("/", function(req, res) {
const email = req.body.email;
// console.log(emailEntry);
const data = {
members: [{
email_address: email,
status: "subscribed"
}]
};
const jsonData = JSON.stringify(data)
const options = {
url: "https://usX.api.mailchimp.com/3.0/lists/LIST_ID",
method: "POST",
headers: {
Authorization: "auth API_KEY"
},
body: jsonData
}
request(options, function(err, response, body) {
if (err) {
res.redirect("tryagain");
} else {
if (response.statusCode === 200) {
res.redirect("success");
} else {
res.redirect("tryagain");
}
}
});
});
Upvotes: 1