[英]Multiple axios requests
我目前正在從事一個項目,我將結帳頁面與 Klarna 集成在一起。 集成從 axios POST 請求(創建訂單)開始。 這個 POST 請求給了我一個響應,包括一個 Iframe html_snippet 和一個訂單 ID。 在這個發布請求之后,我應該向 Klarna 的 rest API 發送一個 GET 請求(確認頁面),其中來自早期 POST 請求的訂單 ID 將在 GET 請求的 URL 中。 問題是我不知道如何鏈接這些請求才能在 GET 請求 URL 中使用來自 POST 請求的response.data.order_id
。 到目前為止,這是我的代碼。 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)
})
我該怎么辦?
您可以將整個代碼包裝在一個async
函數中。 然后你可以做這樣的事情:
var Getrequest = await axios(config1);
var Postrequest = await axios(config);
// Do something with the data
await
停止整個函數,所以它會同步。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.