[英]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.