我正在使用REST API和AXIOS获取产品详细信息我正在控制台日志中接收数据,但无法在要显示的单个产品页面中显示数据
请帮助我解决此错误
这是我的单一产品页面
import { Add, Remove } from '@mui/icons-material';
import React, { useEffect, useState } from 'react'
import { useLocation } from 'react-router-dom';
import styled from 'styled-components'
import Announcement from '../components/Announcement';
import Footer from '../components/Footer';
import Navbar from '../components/navbar';
import Newsletter from '../components/Newsletter';
import { mobile } from '../Responsive';
import { publicRequest } from '../requestMethods';
// import { addProduct } from '../redux/cartRedux';
// import { useDispatch } from 'react-redux';
const Container = styled.div``;
const Wrapper = styled.div`
padding: 50px;
display: flex;
${mobile({padding: "10px", flexDirection:"column"})}
`;
const ImgContainer = styled.div`
flex: 1;
`;
const Image = styled.img`
width: 100%;
height: 90vh;
object-fit: cover;
${mobile({height: "40vh"})}
`;
const InfoContainer = styled.div`
flex: 1;
padding: 0 50px;
${mobile({padding: "10px"})}
`;
const Title = styled.h1`
font-weight: 200;
`;
const Desc = styled.p`
margin: 20px 0;
`;
const Price = styled.span`
font-size: 40px;
font-weight: 100;
`;
const FilterContainer = styled.div`
width: 50%;
display: flex;
justify-content: space-between;
margin: 30px 0;
${mobile({width: "100%"})}
`;
const Filter = styled.div`
display: flex;
align-items: center;
`;
const FilterTitle = styled.span`
font-size: 28px;
font-weight: 20;
`;
const FilterColor = styled.div`
width: 20px;
height: 20px;
border-radius: 50%;
background-color: ${props=>props.color};
margin: 0 5px;
cursor: pointer;
`;
const FilterSize = styled.select`
margin-left: 10px;
padding: 5px;
`;
const FilterSizeOption = styled.option``;
const AddContainer = styled.div`
width: 50%;
display: flex;
align-items: center;
justify-content: space-between;
${mobile({width: "100%"})}
`;
const AmountContainer = styled.div`
display: flex;
align-items: center;
font-weight: 700;
`;
const Amount = styled.span`
width: 30px;
height: 30px;
border-radius: 10px;
border: 1px solid teal;
display: flex;
justify-content: center;
align-items: center;
margin: 0 5px;
`;
const Button = styled.button`
padding: 15px;
border: 2px solid teal;
background-color: white;
cursor: pointer;
font-weight: 500;
&:hover{
background-color: #f8f4f4;
}
`;
const Product = () => {
const location = useLocation();
const id = location.pathname.split("/")[2];
const [product, setProduct] = useState({});
useEffect(() => {
const getProduct = async () => {
try {
const res = await publicRequest.get("/products/find/" + id);
setProduct(res.data);
console.log(res);
} catch(error) {
console.log(error);
}
};
getProduct();
}, [id]);
return (
<Container>
<Navbar/>
<Announcement/>
<Wrapper>
<ImgContainer>
<Image src= {product.img} />
</ImgContainer>
<InfoContainer>
<Title>{product.title}
</Title>
<Desc>{product.desc}</Desc>
<Price>{product.price}</Price>
<FilterContainer>
<Filter>
<FilterTitle>Color</FilterTitle>
<FilterColor color="black" />
<FilterColor color="darkblue"/>
<FilterColor color="gray"/>
</Filter>
<Filter>
<FilterTitle>Size</FilterTitle>
<FilterSize>
<FilterSizeOption>XS</FilterSizeOption>
<FilterSizeOption>S</FilterSizeOption>
<FilterSizeOption>M</FilterSizeOption>
<FilterSizeOption>L</FilterSizeOption>
<FilterSizeOption>XL</FilterSizeOption>
<FilterSizeOption>XXL</FilterSizeOption>
</FilterSize>
</Filter>
</FilterContainer>
<AddContainer>
<AmountContainer>
<Remove/>
<Amount>1</Amount>
<Add/>
</AmountContainer>
<Button>Add to cart</Button>
</AddContainer>
</InfoContainer>
</Wrapper>
<Newsletter/>
<Footer/>
</Container>
)
}
export default Product
这是我的AXIOS请求方法
import axios from "axios";
const BASE_URL = "http://localhost:5000/api/";
const TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzZjdiNTk5OTliYjNkMzI2MGU4ZGY0NSIsImlzQWRtaW4iOnRydWUsImlhdCI6MTY3ODQzNzI5NywiZXhwIjoxNjc4Njk2NDk3fQ.4ncPTwHxOPKHxjK3m8LgmN04NzdfBWtvKiLoce6b20Q";
export const publicRequest = axios.create({
baseURL: BASE_URL,
});
export const userRequest = axios.create({
baseURL: BASE_URL,
header: {token: `Bearer ${TOKEN}`}
})
This is my console log where i received the data
2条答案
按热度按时间dgiusagp1#
您应该将
setProduct(res.data);
更改为setProduct(res.data.product);
从您收到的数据的屏幕截图来看,结构如下所示
但是,您在
useEffect
中调用的setProduct
函数将data
的整个有效负载分配给product
变量,这是不正确的。mrwjdhj32#
下面的代码可以帮助您:
另一项变更如下: