I am trying to create an app where I sign on clients to mailchimp and when I try to sign them up I get this error message:
ReferenceError: response is not defined
at C:\Users\Owner\Desktop\Newsletter-Signup\app.js:57:3
at Layer.handle [as handle_request] (C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\express\lib\router\index.js:284:15
at Function.process_params (C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\express\lib\router\index.js:346:12)
at next (C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\express\lib\router\index.js:280:10)
at C:\Users\Owner\Desktop\Newsletter-Signup\node_modules\body-parser\lib\read.js:137:5
at AsyncResource.runInAsyncScope (async_hooks.js:193:9)
Here is my code at app.js
//require installed node packages
const express = require("express");
const https = require("https");
//create new express app
const app = express();
//enable express to access static files in the folder called "Public"
app.use(express.static("Public"));
//enable express to parse URL-encoded bodies
app.use(express.urlencoded({
extended: true
}));
//send html file to browser on request
app.get("/", function(req, res) {
res.sendFile(__dirname "/signup.html");
});
//require mailchimp
const client = require("@mailchimp/mailchimp_marketing"); //note that "mailchimp-marketing" causes errors
client.setConfig({
apiKey: "f628d1e9327aaf60e1c9873ff13787f2-us21",
server: "us21",
});
app.post("/", function(req, res) {
//set up constants linked to html form input 'name' attributes
const firstName = req.body.fName;
const lastName = req.body.lName;
const email = req.body.email;
//code from Node snippet linked above, but with members values filled in as described in Angela's video
const run = async () => {
const response = await client.lists.batchListMembers("347eed265e", {
members: [{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}],
});
console.log(response);
};
if (response.statusCode === 200){
res.send("Successfully subscribed!");
} else {
res.send("There was an error with signing up, please try again!");
}
run();
});
//use express app to listen on 3000 and log when it's working
app.listen(3000, function() {
console.log("Server is running on port 3000.")
});
// API KEY
// f628d1e9327aaf60e1c9873ff13787f2-us21
// List ID
// 347eed265e
I was wondering if you can help me figure out any errors in my code Thanks, Mark
I tried to be able to sign up a client by putting the persons name and address in the field and hoping to get the response "Succussfully subscribed", instead of getting the error that I received.
Here is what my signup page looks like, when I try to sign up a client:
[Here is my form page for my app(https://i.stack.imgur.com/ok5iE.png)
CodePudding user response:
response
is only accessible within the function scope of run()
. You have an if
statement that references response
outside of the scope where it is defined:
const run = async () => {
const response = await client.lists.batchListMembers('347eed265e', {
// <----------------- response scope start
members: [
{
email_address: email,
status: 'subscribed',
merge_fields: {
FNAME: firstName,
LNAME: lastName,
},
},
],
});
console.log(response);
// <----------------- response scope end
};
if (response.statusCode === 200) {
res.send('Successfully subscribed!');
} else {
res.send('There was an error with signing up, please try again!');
}
run();
Hope this helps.
CodePudding user response:
As damonholden said your response is out scope inside the run() function, also I don't quite understand why you would wrap your code in the run function.
You should use the promise as it is :
app.post("/", function(req, res) {
//set up constants linked to html form input 'name' attributes
const firstName = req.body.fName;
const lastName = req.body.lName;
const email = req.body.email;
client.lists.batchListMembers('347eed265e',
{
members: [
{
email_address: email,
status: 'subscribed',
merge_fields: {
FNAME: firstName,
LNAME: lastName,
},
},
],
}).then(response => {
console.log(response);
if (response.statusCode === 200) {
res.send('Successfully subscribed!');
} else {
res.send('There was an error with signing up, please try again!');
}
}).catch(() => {
res.send('There was an error with signing up, please try again!');
});
});