Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 反应&;Axios-从对象内部的对象获取值_Reactjs_Axios - Fatal编程技术网

Reactjs 反应&;Axios-从对象内部的对象获取值

Reactjs 反应&;Axios-从对象内部的对象获取值,reactjs,axios,Reactjs,Axios,我在这里做一个小项目,当我开始使用API时,我被卡住了 因此,我面临的问题是,我需要访问api中的“价格”部分。以下是api中一个产品的示例: 我已经设法进入了0:Object等,因此我可以访问name和brandName等。但我还需要进入那里的price 我自己做了一些尝试,但没有一次成功 这是我的密码: import axios from "axios"; import { useState } from "react"; const ApiTes

我在这里做一个小项目,当我开始使用API时,我被卡住了

因此,我面临的问题是,我需要访问api中的“价格”部分。以下是api中一个产品的示例:

我已经设法进入了
0:Object
等,因此我可以访问
name
brandName
等。但我还需要进入那里的
price

我自己做了一些尝试,但没有一次成功

这是我的密码:

import axios from "axios";
import { useState } from "react";

const ApiTest = (props) => {
  const [product, setProducts] = useState([]);
  const [price, setPrice] = useState([]);

  const getProducts = () => {
    const options = {
      method: "GET",
      url: "https://asos2.p.rapidapi.com/products/v2/list",
      params: {
        store: "US",
        offset: "0",
        categoryId: "4209",
        limit: "48",
        country: "US",
        sort: "freshness",
        currency: "USD",
        sizeSchema: "US",
        lang: "en-US",
      },
      headers: {
        "x-rapidapi-key": "",
        "x-rapidapi-host": "",
      },
    };

    axios
      .request(options)
      .then(function (response) {
        setProducts(response.data.products);
        console.log(response.data.products);
      })
      .catch(function (error) {
        console.error(error);
      });
  };

  return (
    <div>
      <button onClick={getProducts}>Get Products</button>
      <div>
        <ul>
          {product.map((post) => (
            <li key={post.id}>{post.brandName}</li>
          ))}
        </ul>
      </div>
    </div>
  );
};

export default ApiTest;
从“axios”导入axios;
从“react”导入{useState};
常量ApiTest=(道具)=>{
const[product,setProducts]=useState([]);
const[price,setPrice]=useState([]);
const getProducts=()=>{
常量选项={
方法:“获取”,
url:“https://asos2.p.rapidapi.com/products/v2/list",
参数:{
商店:“美国”,
偏移量:“0”,
类别:“4209”,
限制:“48”,
国家:“美国”,
排序:“新鲜度”,
货币:“美元”,
sizeSchema:“美国”,
朗:“恩,我们”,
},
标题:{
“x-rapidapi-key”:“,
“x-rapidapi-host”:“,
},
};
axios
.请求(选项)
.然后(功能(响应){
setProducts(响应、数据、产品);
日志(响应、数据、产品);
})
.catch(函数(错误){
控制台错误(error);
});
};
返回(
获得产品
    {product.map((post)=>(
  • {post.brandName}
  • ))}
); }; 导出默认测试;
您只需要:


products[0].price.currency

您能提供从API得到的完整响应吗?另外,您是说
post.price.currency
例如不起作用吗?@RonB。张贴在pastebin上:
yP8nXTSc
@Nisarthy,它确实有效。奇怪的是,我之前在谷歌chrome上试用过,但后来改用Mozilla来设计页面。它是有效的。谢谢对披头士经典歌曲的奇怪理解,但正确