【发布时间】:2021-08-18 04:52:28
【问题描述】:
我在 JS 方面完全是绿色的,但我有一个小问题。我想创建包含汽车信息的动态表。我从我的朋友 API 获取数据(我将在帖子底部显示完整的 json),我可以在日志控制台中显示它。但我不知道如何将其插入表中。这是我的代码:
const CarsClient = () => {
const [car, setCars] = useState({});
const getCarsList = async () => {
const response = await fetch('http://localhost:3000/api/catalog/car?fbclid=IwAR0lOlJP-1kBKvQzn8GQ_3oYqVN5kTbRrlctDO6T10CJcumcPWfa7WVugQw');
const jsonData = await response.json();
setCars(jsonData);
console.log(jsonData);
};
useEffect(() => {
getCarsList();
}, []);
(...)
}
const renderCars = (car, index) => {
return(
<tr key={index}>
<td>{car.id}</td>
<td>{car.cost}</td>
)
}
return (
<>
<Container className="cont">
(...)
<ReactBootStrap.Table striped bordered hover>
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{
car.map(renderCars)
}
</tbody>
</ReactBootStrap.Table>
</Col>
</Row>
</Container>
</>
)
} 导出默认 CarsClient
这是 JSON: {"car":[{"id":1,"cost":250,"VIN":2334433,"availability":true,"CarModelId":1,"CarModel":{"id":1,"name ":"X5","fuel":"ON","body":"SUV","productionYear":2020,"enginePower":200,"MakeId":1,"Make":{"id":1 ,"名称":"BMW"}}},{"id":2,"cost":275,"VIN":2334433,"availability":true,"CarModelId":2,"CarModel":{"id ":2,"name":"Q7","fuel":"ON","body":"SUV","productionYear":2019,"enginePower":220,"MakeId":2,"Make": {"id":2,"name":"AUDI"}}}]}
我有这个错误:CarsClient.js:96 Uncaught TypeError: car.map is not a function
【问题讨论】:
标签: javascript reactjs bootstrap-4 fetch react-bootstrap