Home > Enterprise >  Uncaught TypeError: Cannot read properties of undefined (reading 'image and name') in Reac
Uncaught TypeError: Cannot read properties of undefined (reading 'image and name') in Reac

Time:02-28

So, I want to display specific product page using id as the parameter, it seems to match the id just fine but when i try to display the product image and name that have the product id (let's say id=1 and so on) it displays error in developer tools console and said that the properties of product.name and product.image are undefined.

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;

this code here should display the product image and name based on the url id, let's say localhost:3000/products/{id} and the id is 1, but it display error like the title above.

<Row>
    <Col md={6}>
       <Image src={product.image} alt={product.name}/>
    </Col>
</Row>

This is the products.js that i want to access by using the properties.

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

CodePudding user response:

The problem is with this line

const product = products.find((p) => p._id === Number(id));

_id in your data is a string and you are converting id from useParams to a Number

so you might want to do either of the two things

const product = products.find((p) => Number(p._id) === Number(id));

or

const product = products.find((p) => p._id === id);

or if you are a Type opposer and literally hate datatypes(:P) you can do this

const product = products.find((p) => p._id == Number(id));

OR

if you just love Typed languages like me then you can move to Typescript and save a lot of time in the future.

  • Related