我正在使用react从starwars api(https://swapi.co/)获取数据。它具有homeworld属性,该属性调用另一个api来获取有关homeworld的详细信息。我尝试在另一个axios.get()中使用axios.get()来获取 "${results.homeworld}"
(PFA代码)的详细信息,但它不会返回任何内容。不知道为什么会这样,即使我更改axios.get请求,我也无法更新状态。这是我的首次搜索,即卢克·天行者。我应该如何更改每个请求的状态以及如何处理初始JSON对象内的homeworld API。此外,我还收到错误意外的令牌<在JSON中的位置0 的错误,确实尝试了在线帮助那个,但还是一样的问题。
import React from "react";
import axios from "axios";
class Axiosa extends React.Component {
constructor(props) {
super(props);
this.state = { data: {}, name: "", homeplanet: "", species: "" };
}
fetchStarwars() {
axios
.get("https://swapi.co/api/people/3/")
//.then(results => results.text())
.then(results => results.json())
.then(results => {
let x = `"${results.homeworld}"`;
console.log(`"${results.homeworld}"`);
axios
.get(x)
.then(resultshomeworld => {
resultshomeworld.json();
console.log(resultshomeworld);
})
.then(results => {
this.setState({ homeplanet: results.name });
});
this.setState({ data: results, name: results.name, isLoading: false });
});
}
componentDidMount() {
this.fetchStarwars();
}
render() {
console.log(this.state.homeplanet);
return (
<div className="App">
<span>{this.state.name}</span>
<div>{this.state.homeplanet}</div>
</div>
);
}
}
export default Axiosa;
答案 0 :(得分:0)
在构造函数中绑定fetchStarwars函数
constructor(props) {
super(props);
this.state = { data: {}, name: "", homeplanet: "", species: "" };
this.fetchStarwars = this.fetchStarwars.bind(this);
}
答案 1 :(得分:0)
尝试从第一个then
中的第二个return Promise.resolve(result.json());
块开始处理所有工作,或者拥有第一个fetchStarwars() {
axios.get("https://swapi.co/api/people/3/")
.then(result => {
let results = result.json();
let x = `"${results.homeworld}"`;
console.log(`"${results.homeworld}"`);
axios.get(x)
.then(results => {
this.setState({ homeplanet: results.json().name });
});
this.setState({
data: results,
name: results.json().name,
isLoading: false
});
});
}
,以便它可以访问减少的promise值。
对嵌套的第二个axios调用执行相同的操作。
this
还要确保在构造函数中也将fetchStarwars
绑定到$hook_to = 'woocommerce_thankyou';
$what_to_hook = 'add2postweb';
$prioriy = 111;
$num_of_arg = 1;
add_action($hook_to, $what_to_hook, $prioriy, $num_of_arg);
function add2postweb($order_id){
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$order_data = $order->get_data(); // The Order data
$order_payment_method = $order_data['payment_method'];
$order_billing_first_name = $order_data['billing']['first_name'];
$order_billing_last_name = $order_data['billing']['last_name'];
$order_billing_company = $order_data['billing']['company'];
$order_billing_address_1 = $order_data['billing']['address_1'];
$order_billing_address_2 = $order_data['billing']['address_2'];
$order_billing_city = $order_data['billing']['city'];
$order_billing_state = $order_data['billing']['state'];
$order_billing_postcode = $order_data['billing']['postcode'];
$order_billing_country = $order_data['billing']['country'];
$order_billing_email = $order_data['billing']['email'];
$order_billing_phone = $order_data['billing']['phone'];
$stuff_id=array();
$stuff_count=array();
// Iterating through each WC_Order_Item_Product objects
foreach ($order->get_items() as $item_key => $item_values):
## Access Order Items data properties (in an array of values) ##
$item_data = $item_values->get_data();
$product_name = $item_data['name'];
$product_id = $item_data['product_id'];
$variation_id = $item_data['variation_id'];
$quantity = $item_data['quantity'];
$tax_class = $item_data['tax_class'];
$line_subtotal = $item_data['subtotal'];
$line_subtotal_tax = $item_data['subtotal_tax'];
$line_total = $item_data['total'];
$line_total_tax = $item_data['total_tax'];
$username = '*******';
$password = '*******';
$client = new SoapClient('http://svc.ebazaar-post.ir/EshopService.svc?wsdl');
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;
$result = $client -> AddStuff ([
'username' => $username,
'password' => $password,
'name' => $product_name,
'price' => 501111000,
'weight' => 2010,
'count' => 1100,
'description' => 'تست',
'percentDiscount' => 0,
]);
$stuff_id=(int)$result->AddStuffResult;
$count=(int)$quantity;
endforeach;
echo'<<<<<<<<<<';
echo $stuff_id;
echo $count;
echo '>>>>>>>>>>';
$username = 'borujerd';
$password = 'Borujerd20#$';
$client = new SoapClient('http://svc.ebazaar-post.ir/EshopService.svc?wsdl');
$client->soap_defencoding = 'UTF-8';
$client->decode_utf8 = false;
$result2 = $client -> AddParcel ([
'username' => $username,
'password' => $password,
'productsId' => [
[
'Id' => $stuff_id, // Product id in ebazaar
'Count' => $count, // Request Number Of Product
'DisCount' => 0 // No Discount
]
],
'cityCode' => 1,
'serviceType' => 1, // pishtaz
'payType' => 1,
'registerFirstName' => $order_billing_first_name,
'registerLastName' => $order_billing_last_name,
'registerAddress' => 'نشانی دریافت کننده',
'registerPhoneNumber' => '0912111111',
'registerMobile' => '0912111111',
'registerPostalCode' => '1648853978'
]);
}
。
答案 2 :(得分:0)
H对您的代码进行了一些更改。您需要在this
函数中使用fetchStarwars
,这可以通过Nitha所说的bind
或使用箭头函数来实现。
请参阅工作示例here
import React from "react";
import axios from "axios";
import {render} from "react-dom"
class Axiosa extends React.Component {
constructor(props) {
super(props);
this.state = { data: {}, name: "", homeplanet: "", species: "" };
}
fetchStarwars = () => {
axios
.get("https://swapi.co/api/people/3/")
.then(response => {
let results = response.data
let x = results.homeworld;
this.setState({ data: results, name: results.name, isLoading: false });
return axios.get(x)
})
.then(results => {
this.setState({ homeplanet: results.data.name }); });
}
componentDidMount() {
this.fetchStarwars();
}
render() {
console.log(this.state);
return (
<div className="App">
<span>{this.state.name}</span>
<div>{this.state.homeplanet}</div>
</div>
);
}
}
render(<Axiosa />, document.getElementById('root'));