[英]When I try to run this line of code in “node.js”, I am getting the following error
const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
const https = require('https');
const app = express();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(req, res){
res.sendFile(__dirname + "/signup.html");
});
app.post('/', function(req,res){
const firstName = req.body.fName;
const lastName = req.body.lName;
const email = req.body.email;
const data = {
members: [
{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}
]
};
const jsonData = JSON.stringify(data);
const url ="https://us18.api.mailchimp.com/3.0/lists/081d03d860";
const options ={
method: "POST",
auth: "mick:2c775770a96a720b8c492df7974840d3-us18"
}
const request = https.request(url, options, function(response) {
if (response.statusCode === 200){
response.send('Successfully subscribed');
} else {
response.send('There was an error with singing up, please try again');
}
response.on('data', function(data){
console.log(JSON.parse(data));
});
});
request.write(jsonData);
request.end();
});
app.listen(3000, function(){
console.log('Server is running on port 3000');
});
I am currently using Node.js to creat a newsletter sing up page with mailchimp as my API.When i run the code in node.js i keep getting this line of error.我目前正在使用 Node.js 创建一个使用 mailchimp 作为我的 API 的时事通讯唱歌页面。当我在 node.js 中运行代码时,我不断收到这一行错误。 I am trying to use the mailchimp API.
我正在尝试使用 mailchimp API。 I am creating a newsletter sing up page.
我正在创建一个时事通讯唱歌页面。 But i keep getting this error.
但我不断收到这个错误。 I am running it in node.js.
我在 node.js 中运行它。 Can Someone please help me out.
有人可以帮帮我吗。
TypeError: "listener" argument must be a function
at ClientRequest.once (events.js:340:11)
at new ClientRequest (_http_client.js:164:10)
at Object.request (http.js:38:10)
at Object.request (https.js:239:15)
at C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\app.js:40:24
at Layer.handle [as handle_request] (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Desmond\Desktop\Web Development\Newsletter-Singup\node_modules\express\lib\router\index.js:281:22
In reading the doc of 'https' module, I can see that the arguments shown are:在阅读“https”模块的文档时,我可以看到显示的 arguments 是:
Could you try to change your code by this one?你能试着用这个来改变你的代码吗?
app.post('/', function(req, res) {
const firstName = req.body.fName;
const lastName = req.body.lName;
const email = req.body.email;
const data = {
members: [{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: firstName,
LNAME: lastName
}
}
]
};
const jsonData = JSON.stringify(data);
const options = {
hostname: 'us18.api.mailchimp.com',
path: "/3.0/lists/081d03d860",
method: "POST",
auth: "mick:2c775770a96a720b8c492df7974840d3-us18"
}
const request = https.request(options, function(response) {
if (response.statusCode === 200) {
response.send('Successfully subscribed');
} else {
response.send('There was an error with singing up, please try again');
}
response.on('data', function(data) {
console.log(JSON.parse(data));
});
});
request.write(jsonData);
request.end();
});
Inside of this:这里面:
app.post('/', function(req, res) { // <== Note the response here is named res
You have this:你有这个:
const request = https.request(options, function(response) {
if (response.statusCode === 200) {
response.send('Successfully subscribed'); // <== This is trying to send a
// response to your response which
// doesn't make sense
} else {
response.send('There was an error with singing up, please try again');
}
This does not make sense.这根本不符合逻辑。 You don't call
response.send()
on the http.request
response that you just got.您不会在刚刚收到的
http.request
响应上调用response.send()
。 That http request is done. http 请求已完成。 You made a request and got a response.
您提出了请求并得到了响应。
I would presume, you mean for that code to be this where you're sending a response from your server back to the original client request:我想,您的意思是该代码是您将服务器的响应发送回原始客户端请求的地方:
const request = https.request(options, function(response) {
if (response.statusCode === 200) {
res.send('Successfully subscribed'); // <== use res here so it refers to the
// response to the original Express
// request/response
} else {
res.send('There was an error with singing up, please try again');
}
FYI, you may want to consider that the request()
library has been deprecated and should generally not be used for new code.仅供参考,您可能需要考虑
request()
库已被弃用,通常不应用于新代码。 There is a list of good alternatives here .这里有一个不错的选择列表。 I'm personally using
got()
as I found it to be simple to use, fully supports promises (which is how you should be programming asynchronous code these days) and contains the features I need.我个人使用
got()
,因为我发现它使用简单,完全支持 Promise(这就是你现在应该如何编写异步代码)并且包含我需要的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.