简体   繁体   中英

JSON error in NodeJS Node-Fetch Call Stripe

For Some Reason, I am getting this error when using stripe API in node-fetch. Error: TypeError: Cannot use 'in' operator to search for 'signal' in

{"method":"post","headers":{"Content-Type":"application/json","Authorization":"Bearer API KEY"},"body":{"type":"card","card[number]":"4000000000000077","card[exp_month]":"01","card[exp_year]":"2023","card[cvc]":"235"}}

When I try other solutions and JSON.parse() it, then I get an invalid token error.

Here is the code to reproduce the problem:

async function payCreator(product){
  var price1 = product.price.replace("$","")
   var price2 = price1.replace(".","")
   var price3 = price2 *product.quantity

 //charge customer
  var url = "https://api.stripe.com/v1/payment_methods";
  var params1 = {
    method: "post",
    headers: headers,
    body: {"type": "card", "card[number]": product.card,"card[exp_month]": product.month,"card[exp_year]": product.year,"card[cvc]": product.cvc}
  };
var params2 = JSON.stringify(params1)
console.log(params2)
var params = params1

     var res = await fetch(url, (params2));
    var result = await res.json();
    return JSON.stringify(result)

Any solution appreciated

I think you can't pass a String as a fetch parameter. I think it can be a JavaScript Object. So, try this:

  1. Stringify body attribute of params1

 var bodyContent = JSON.stringify({"type" : "card", "card[number]" : product.card, "card[exp_month]" : product.month, "card[exp_year]" : product.year, "card[cvc]" : product.cvc}); var params1 = { method: "post", headers: headers, body: bodyContent };

  1. Pass params1 as parameter of fetch

 var res = await fetch(url, params1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM