收到此错误 TypeError [ERR_INVALID_ARG_TYPE]: 第一个参数必须是字符串类型或 Buffer 或 Uint8Array 的实例

Getting this error TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array

我正在尝试使用支付网关 API 并创建订阅,但我在标题中遇到此错误。我试过调试,但一切都不知道该怎么做。无论我做什么,我都会收到错误消息。

一切都很好,表单也很好,只是在单击提交时出现此错误。

我 运行 在我的 PC 上的本地主机上。

这是错误:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Object

这是我的 app.js 代码:

const express = require("express");
const https = require('https');
const bodyParser = require("body-parser");
var request = require('request');



const app = express();

app.get("/", function(req, res) {

  // https.get(url, function(response) {
  //   console.log(response.statusCode);
  // })
  res.sendFile(__dirname + "/index.html");


})

app.use(express.static("public"));
app.use(bodyParser.urlencoded({
  extended: true
}));

app.post("/", function(req, res) {

  const customerEmailV = req.body.email;
  const customerPhoneV = req.body.pNumber;
  const subscriptionIdV = req.body.sID;
  const planIdV = "001";
  const returnUrlV = "https://google.com";
  const paymentOptionV = "card";
  const cardNumberV = req.body.cNumber;
  const card_expiryMonthV = req.body.expiryM;
  const card_expiryYearV = req.body.expiryYYYY;
  const card_cvvV = req.body.cvv;
  const card_holderV = req.body.cardHolderName;
  
  // console.log(customerEmail, customerPhone,subscriptionId,planId,returnUrl,paymentOption,cardNumber,card_expiryMonth);
  // console.log(card_expiryYear,card_holder,card_cvv);

  // const dataJ = {
  //   customerEmail: customerEmailV,
  //   customerPhone: customerPhoneV,
  //   returnUrl: returnUrlV,
  //   subscriptionId: subscriptionIdV,
  //   planId: planIdV,
  //   paymentOption: paymentOptionV,
  //   card_number: card_numberV,
  //   card_expiryMonth: card_expiryMonthV,
  //   card_expiryYear: card_expiryYearV,
  //   card_cvv: card_cvvV,
  //   card_holder: card_holderV
  // };

  const url = "https://test.cashfree.com/api/v2/subscriptions";

  const options = {
    'method': 'POST',
    'url': 'https://test.cashfree.com/api/v2/subscriptions',
    'headers': {
      'X-Client-Id': '9353760f74de0be3bf586b67d73539',
      'X-Client-Secret': 'ee7a9d1ddbb245572990f9ba7e6de3995a0a8cfd',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      "subscriptionId": subscriptionIdV,
      "planId": planIdV,
      "customerEmail": customerEmailV,
      "customerPhone": customerPhoneV,
      "expiresOn": "2022-05-30 23:59:59",
      "paymentOption": paymentOptionV,
      "card_number": cardNumberV,
      "card_expiryMonth": card_expiryMonthV,
      "card_expiryYear": card_expiryYearV,
      "card_cvv": card_cvvV,
      "card_holder": card_holderV,
      "firstChargeDate": "2021-10-06",
      "returnUrl": "https://google.com"
    })

  };


  const request = https.request(options, function(response) {
    response.on("data", function(data) {
      console.log(JSON.parse(data));
    });
  });

  request.write();
  request.end();

})


app.listen(3000, function(req, res) {
  console.log("The server is running at port 3000")
})

这是我的 HTML 代码:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- bootstrap -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">

  <!-- CSS -->
  <link rel="stylesheet" href="css/style.css">


  <title>Zingy Subscription</title>
</head>

<body>

  <section id="title">

    <!-- As a heading -->
    <nav class="navbar navbar-light bg-light">
      <div class="container-fluid">
        <span class="navbar-brand mb-0 h1">Zingy</span>
      </div>
    </nav>

  </section>

  <section id="form">


    <h1>Zingy Subscription</h1>
    <p>2,999Rs. Per Year</p>
    <form action="/", method="POST">
      <div class="mb-3">
        <input type="email" name="email" class="form-control" aria-describedby="emailHelp" placeholder="Enter email address" required autofocus>
        <!-- <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div> -->
      </div>
      <div class="mb-3">
        <input type="text" name="pNumber" class="form-control" placeholder="Enter 10 digit Phone Number" required>
      </div>

      <div class="mb-3">
        <input type="text" name="sID" class="form-control" placeholder="Subscription ID" required>
      </div>

      <!-- <div class="mb-3">
        <input type="text" name="pID" class="form-control" placeholder="Plan ID" required>
      </div> -->
      <!-- <div class="mb-3">
        <input type="text" name="rURL" class="form-control" placeholder="Enter returnUrl" required>
      </div> -->
      <!-- <div class="mb-3">
        <input type="text" name="pOption" class="form-control" placeholder="paymentOption" required>
      </div> -->

      <div class="mb-3">
        <input type="text" name="cNumber" class="form-control" placeholder="Enter Card Number" required>
      </div><div class="mb-3">
        <input type="text" name="expiryM" class="form-control" placeholder="Enter Expiry Month" required>
      </div><div class="mb-3">
        <input type="text" name="expiryYYYY" class="form-control" placeholder="Enter Expiry Year" required>
      </div><div class="mb-3">
        <input type="text" name="cvv" class="form-control" placeholder="Enter CVV" required>
      </div><div class="mb-3">
        <input type="text" name="cardHolderName" class="form-control" placeholder="Enter Card Holder Name" required>
      </div>
      <button type="submit" class="btn btn-primary">Submit</button>
    </form>

  </section>

  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

  <!-- Option 2: Separate Popper and Bootstrap JS -->

  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js" integrity="sha384-W8fXfP3gkOKtndU4JGtKDvXbO53Wy8SZCQHczT5FMiiqmQfUpWbYdTil/SxwZgAN" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.min.js" integrity="sha384-skAcpIdS7UcVUC05LJ9Dxay8AXcDYfBJqt1CJ85S/CFujBsIzCIv+l9liuYLaMQ/" crossorigin="anonymous"></script>



</body>

</html>

得到答案。

专业提示:您可以使用 Postman 以您的首选语言为您生成代码。

const express = require('express');
const https = require('follow-redirects').https;
const bodyParser = require("body-parser");
const fs = require('fs');



const app = express();

app.use(express.static("public"));
app.use(bodyParser.urlencoded({
  extended: true
}));

app.get("/", function(req, res) {

  res.sendFile(__dirname + "/index.html");

})



app.post("/", function(req, res) {
  const customerEmailV = req.body.email;
  const customerPhoneV = req.body.pNumber;
  const subscriptionIdV = req.body.sID;
  const planIdV = "001";
  const returnUrlV = "https://google.com";
  const paymentOptionV = "card";
  const cardNumberV = req.body.cNumber;
  const card_expiryMonthV = req.body.expiryM;
  const card_expiryYearV = req.body.expiryYYYY;
  const card_cvvV = req.body.cvv;
  const card_holderV = req.body.cardHolderName;

  var options = {
    'method': 'POST',
    'hostname': 'test.cashfree.com',
    'path': '/api/v2/subscriptions',
    'headers': {
      'X-Client-Id': '9353760f74de0be3bf586b67d73539',
      'X-Client-Secret': 'ee7a9d1ddbb245572990f9ba7e6de3995a0a8cfd',
      'Content-Type': 'application/json'
    },
    'maxRedirects': 20
  };

  var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function (chunk) {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });

  res.on("error", function (error) {
    console.error(error);
  });
});

var postData = JSON.stringify({
  "subscriptionId": subscriptionIdV,
  "planId": planIdV,
  "customerEmail": customerEmailV,
  "customerPhone": customerPhoneV,
  "expiresOn": "2022-05-30 23:59:59",
  "paymentOption": paymentOptionV,
  "card_number": cardNumberV,
  "card_expiryMonth": card_expiryMonthV,
  "card_expiryYear": card_expiryYearV,
  "card_cvv": card_cvvV,
  "card_holder": card_holderV,
  "firstChargeDate": "2021-10-06",
  "returnUrl": "https://google.com"
});

req.write(postData);

req.end();



   console.log(customerEmailV, customerPhoneV,subscriptionIdV,planIdV,returnUrlV,paymentOptionV,cardNumberV,card_expiryMonthV);
  // console.log("we got it");
})


app.listen(3000, function() {
  console.log("The server is running at port 3000");
})