首页 > 解决方案 > 多个 axios 请求

问题描述

我目前正在开展一个项目,我正在将结帐页面与 Klarna 集成。集成从 axios POST 请求(创建订单)开始。这个 POST 请求给了我一个响应,其中包括一个 Iframe html_snippet 和一个订单 ID。在这个发布请求之后,我应该向 Klarna 的其余 API 发送一个 GET 请求(确认页面),其中来自早期 POST 请求的订单 ID 将位于 GET 请求的 URL 中。问题是我不知道如何链接这些请求以便能够使用response.data.order_idGET 请求 URL 中的 POST 请求。到目前为止,这是我的代码。axios.all()显然不会工作,因为请求是同时发送的。

var axios = require("axios");
const { promiseImpl } = require("ejs");
const { response } = require("express");
 var data = JSON.stringify({
   purchase_country: "SE",
   purchase_currency: "SEK",
   locale: "sv-SE",
   order_amount: 50000,
   order_tax_amount: 4545,
   order_lines: [
     {
       type: "physical",
       reference: "19-402-USA",
       name: "Red T-Shirt",
       quantity: 5,
       quantity_unit: "pcs",
       unit_price: 10000,
       tax_rate: 1000,
       total_amount: 50000,
       total_discount_amount: 0,
       total_tax_amount: 4545,
     },
   ],
   merchant_urls: {
     terms: "https://www.example.com/terms.html",
     checkout: "https://atelierdecosmetique.herokuapp.com/checkout",
     confirmation: "https://atelierdecosmetique.herokuapp.com/confirmation",
     push: "https://www.example.com/api/push",
   },
 });

 var config = {
   method: "post",
   url: "https://api.playground.klarna.com/checkout/v3/orders/",
   headers: {
     "Content-Type": "application/json",
     Authorization:
       "",
   },
   data: data,
 };


 //GET Request (read order)
var axios = require("axios");
var data1 = JSON.stringify({
  purchase_country: "SE",
  purchase_currency: "SEK",
  locale: "sv-SE",
  order_amount: 50000,
  order_tax_amount: 4545,
  order_lines: [
    {
      type: "physical",
      reference: "19-402-USA",
      name: "Red T-Shirt",
      quantity: 5,
      quantity_unit: "pcs",
      unit_price: 10000,
      tax_rate: 1000,
      total_amount: 50000,
      total_discount_amount: 0,
      total_tax_amount: 4545,
    },
  ],
  merchant_urls: {
    terms: "https://www.example.com/terms.html",
    checkout: "https://www.example.com/checkout.html",
    confirmation: "https://www.example.com/confirmation.html",
    push: "https://www.example.com/api/push",
  },
});

var config1 = {
  method: "get",
  url:
    "https://api.playground.klarna.com/checkout/v3/orders/a94f3f40-df22-6334-95d8-eebb2cf8d986",
  headers: {
    "Content-Type": "application/json",
    Authorization: "",
  },
  data: data1,
};



 var Getrequest = axios(config1);
 var Postrequest = axios(config)

axios.all([
     Postrequest,
     Getrequest
 ])
.then(function(response){
    console.log(response[0].data.order_id)
    app.get("/checkout", function(req, res){
        res.render("checkout.ejs", {datapost: response[0].data.html_snippet})
    })
    app.get("/confirmation", function(req, res){
        res.render("confirmation.ejs", {dataget: response[1].data.html_snippet})
    })


})
.catch(function(err){
    console.log(err)
})

我该怎么办?

标签: javascriptaxios

解决方案


您可以将整个代码包装在一个async函数中。然后你可以做类似的事情:

var Getrequest = await axios(config1);
var Postrequest = await axios(config);

// Do something with the data

await停止整个功能,因此一切都会同步。

文档


推荐阅读