简体   繁体   English

当我尝试在“node.js”中运行这行代码时,我收到以下错误

[英]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 是:

  • request(url, options)请求(网址,选项)
  • request(options, callback)请求(选项,回调)

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.

相关问题 我的服务器已启动,但是当我尝试连接它时,出现错误[Node.js] - My Server is up but when I try to connect to it I am getting an error [Node.js] 当我运行我的 node.js 代码时,我没有得到我想要的输出这里有什么错误? - i am not getting my desired output when i run my node.js code what is the mistake here? 当我尝试运行mt Node.js应用程序时出现TypeError - TypeError when I try to run mt Node.js application 尝试为 togetherjs 应用程序设置集线器服务器,并且我正在使用 node.js 运行服务器时出现以下错误 - Trying to set a hub server for togetherjs application and i am using node.js to run the server i get the following error 我收到此错误: Block is not a constructor in node.js - I am getting this error: Block is not a constructor in node.js 当我尝试在 Visual Studio Code 的终端中使用 Node.js 时,我收到“文档未定义”错误消息 - When I try to use Node.js in Visual Studio Code’s terminal, I get “document is not defined” error messages 我在我的 node.js 代码中收到此错误“无法读取未定义的属性 'split'”。 如何解决这个问题? - I am getting this error “Cannot read property 'split' of undefined” in my node.js code. How to resolve this? 我正在尝试运行 node.js,如何解决“找不到模块”错误? - I am trying to run node.js, how do I resolve "Cannot find module" error? 如何为node.js编写以下代码? - How do I write the following code for node.js? 我收到一个node.js module.js:473错误消息 - I am getting a node.js module.js:473 error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM