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

我目前正在使用 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

在閱讀“https”模塊的文檔時,我可以看到顯示的 arguments 是:

  • 請求(網址,選項)
  • 請求(選項,回調)

你能試着用這個來改變你的代碼嗎?

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

這里面:

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

這根本不符合邏輯。 您不會在剛剛收到的http.request響應上調用response.send() 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()庫已被棄用,通常不應用於新代碼。 這里有一個不錯的選擇列表 我個人使用got() ,因為我發現它使用簡單,完全支持 Promise(這就是你現在應該如何編寫異步代碼)並且包含我需要的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM