当我尝试在“ node.js”中运行此代码行时,出现以下错误

时间:2020-05-25 14:22:03

标签: javascript node.js express

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');
});

我目前正在使用Node.js创建一个以mailchimp作为我的API的新闻通讯页面。当我在node.js中运行代码时,我不断遇到这一行错误。我正在尝试使用mailchimp API。我正在创建一个新闻通讯页面。但是我一直收到这个错误。我在node.js中运行它。有人可以帮帮我吗。

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

2 个答案:

答案 0 :(得分:0)

在阅读“ https”模块的文档时,我可以看到显示的参数是:

  • 请求(URL,选项)
  • 请求(选项,回调)

您可以尝试通过此代码更改代码吗?

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();
});

答案 1 :(得分:0)

在此范围内:

app.post('/', function(req, res) {    // <== Note the response here is named res

你有这个:

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');
    }

这没有道理。您不会在刚刚收到的response.send()响应中致电http.request。该http请求已完成。您提出了请求并得到了答复。

我想,您的意思是该代码就是这样,您将从服务器将响应发送回原始客户端请求:

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');
    }

仅供参考,您可能想考虑request()库已被弃用,通常不应将其用于新代码。有一个list of good alternatives here。我个人使用got()是因为我发现它使用起来很简单,完全支持promises(这是如今你应该如何编程异步代码)并包含我需要的功能。