未捕获的类型错误:尝试从特定 ID 查看图像时,无法在 React JS 中读取未定义的属性(读取“图像和名称”)
2022-02-27
2462
因此,我想使用 id 作为参数来显示特定的产品页面,它似乎与 id 匹配得很好,但是当我尝试显示具有产品 id 的产品图像和名称时(假设 id=1 等等),它会在开发人员工具控制台中显示错误,并说 product.name 和 product.image 的属性未定义。
import React from "react";
import { Link, useParams } from "react-router-dom";
import { Row, Col, Image, ListGroup, Card, Button } from "react-bootstrap";
import Rating from "../components/Rating";
import products from "../products";
const Products = () => {
const { id } = useParams();
const product = products.find((p) => p._id === Number(id));
return (
<>
<Link className="btn btn-light my-3" to="/">
Go Back
</Link>
<Row>
<Col md={6}>
<Image src={product.image} alt={product.name}/>
</Col>
</Row>
</>
);
};
export default Products;
此处的代码应根据 url id 显示产品图像和名称,假设 localhost:3000/products/{id} 且 id 为 1,但它会显示类似上述标题的错误。
<Row>
<Col md={6}>
<Image src={product.image} alt={product.name}/>
</Col>
</Row>
这是我想通过使用属性访问的 products.js。
const products = [
{
_id: '1',
name: 'Airpods Wireless Bluetooth Headphones',
image: '/images/airpods.jpg',
description:
'Bluetooth technology lets you connect it with compatible devices wirelessly High-quality AAC audio offers immersive listening experience Built-in microphone allows you to take calls while working',
brand: 'Apple',
category: 'Electronics',
price: 89.99,
countInStock: 10,
rating: 4.5,
numReviews: 12,
},
{
_id: '2',
name: 'iPhone 11 Pro 256GB Memory',
image: '/images/phone.jpg',
description:
'Introducing the iPhone 11 Pro. A transformative triple-camera system that adds tons of capability without complexity. An unprecedented leap in battery life',
brand: 'Apple',
category: 'Electronics',
price: 599.99,
countInStock: 7,
rating: 4.0,
numReviews: 8,
},
{
_id: '3',
name: 'Cannon EOS 80D DSLR Camera',
image: '/images/camera.jpg',
description:
'Characterized by versatile imaging specs, the Canon EOS 80D further clarifies itself using a pair of robust focusing systems and an intuitive design',
brand: 'Cannon',
category: 'Electronics',
price: 929.99,
countInStock: 5,
rating: 3,
numReviews: 12,
},
{
_id: '4',
name: 'Sony Playstation 4 Pro White Version',
image: '/images/playstation.jpg',
description:
'The ultimate home entertainment center starts with PlayStation. Whether you are into gaming, HD movies, television, music',
brand: 'Sony',
category: 'Electronics',
price: 399.99,
countInStock: 11,
rating: 5,
numReviews: 12,
},
{
_id: '5',
name: 'Logitech G-Series Gaming Mouse',
image: '/images/mouse.jpg',
description:
'Get a better handle on your games with this Logitech LIGHTSYNC gaming mouse. The six programmable buttons allow customization for a smooth playing experience',
brand: 'Logitech',
category: 'Electronics',
price: 49.99,
countInStock: 7,
rating: 3.5,
numReviews: 10,
},
{
_id: '6',
name: 'Amazon Echo Dot 3rd Generation',
image: '/images/alexa.jpg',
description:
'Meet Echo Dot - Our most popular smart speaker with a fabric design. It is our most compact smart speaker that fits perfectly into small space',
brand: 'Amazon',
category: 'Electronics',
price: 29.99,
countInStock: 0,
rating: 4,
numReviews: 12,
},
]
export default products
1个回答
问题出在这行
const product = products.find((p) => p._id === Number(id));
数据中的 _id 是一个字符串,而您正在将 id 从 useParams 转换为数字
因此,您可能需要执行以下两项操作之一
const product = products.find((p) => Number(p._id) === Number(id));
或
const product = products.find((p) => p._id === id);
或者,如果您是类型反对者并且真的讨厌数据类型(:P),您可以这样做
const product = products.find((p) => p._id == Number(id));
或
如果您像我一样喜欢类型化语言,那么您可以转到 Typescript ,并在将来节省大量时间。
Kartik Malik
2022-02-27