我正在尝试创建一个应用程序,让客户端登录到 mailchimp,当我尝试注册它们时,我收到此错误消息:
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)
这是我在 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
我想知道你是否可以帮我找出代码中的任何错误 谢谢, 马克
我尝试通过将人员姓名和地址输入字段来注册客户,并希望得到“成功订阅”的响应,而不是收到我收到的错误。
当我尝试注册客户时,我的注册页面如下所示:
[这是我的应用程序的表单页面(https://i.stack.imgur.com/ok5iE.png)
正如 damonholden 所说,您的响应超出了 run() 函数的范围,我也不太明白为什么您要将代码包装在 run 函数中。
您应该按原样使用承诺:
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!');
});
});
response
只能在run()
的函数范围内访问。您有一个 if
语句,在定义范围之外引用 response
:
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();
希望这有帮助。
正如 damonholden 和 Wiwild 所说,您正在尝试访问其范围之外的
response
变量
要解决应用程序中的问题,您需要将
response.statusCode
比较移至 run
函数
所以你的新代码就变成了
//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.")
});