Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Cannot read properties of undefined (reading 'defaultSize')

I'm trying to useState in my MERN project, and I have defaultSize defined in MongoDB, and am trying to useState by product.defaultSize. If I change some of my code (like adding console.log("hello")) and save it, the error disapears and loads fine. But if I refresh the page the error comes back?

import { useEffect, useState } from "react";

import { getProductDetails } from "../redux/actions/productActions";

import { SizeOptions } from '../components/SizeOptions';

const ProductScreen = ({match}) => {

    const dispatch = useDispatch();

    const productDetails = useSelector(state => state.getProductDetails);
    const { loading, error, product } = productDetails;

    const [ src, setSrc ] = useState(product.defaultSize);


    useEffect(() => {
    if (product && match.params.id !== product._id) {
        dispatch(getProductDetails(match.params.id));
        }
    }, [dispatch, match, product]);
    
    <div className='sizebuttons'>
        {product && (product.size || []).map((size, index) => (<SizeOptions  key={index} size={size} changeSrc={src => setSrc(src)}/>))}
    </div>
    
return (
    <model-viewer
        id="model-viewer"
        src={src}
        alt={product.productName}
        ar
        ar-modes="scene-viewer quick-look"
        ar-placement="floor"
        shadow-intensity="1"
        camera-controls
        min-camera-orbit={product.mincameraorbit} 
        max-camera-orbit={product.maxcameraorbit}
        interaction-prompt="none">  
            <button slot="ar-button" className="ar-button">
                View in your space
            </button>
    </model-viewer>
)};

I've tried also changing it to useState(product?.defaultSize); as I saw another post on here saying that could work, but I had no luck with it. And as you can tell, all the other product.otherstuff works just fine. Any help would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

import { useEffect, useState } from "react";

import { getProductDetails } from "../redux/actions/productActions";

import { SizeOptions } from '../components/SizeOptions';

const ProductScreen = ({match}) => {

    const dispatch = useDispatch();

    const productDetails = useSelector(state => state.getProductDetails);
    const { loading, error, product } = productDetails;

    const [ src, setSrc ] = useState();


    useEffect(() => {
    if (product && match.params.id !== product._id) {
        dispatch(getProductDetails(match.params.id));
        }
    }, [dispatch, match, product]);
    
    <div className='sizebuttons'>
        {product && (product.size || []).map((size, index) => (<SizeOptions  key={index} size={size} changeSrc={src => setSrc(src)}/>))}
    </div>
    
return (
    <model-viewer
        id="model-viewer"
        src={src || product.defaultSize}
        alt={product.productName}
        ar
        ar-modes="scene-viewer quick-look"
        ar-placement="floor"
        shadow-intensity="1"
        camera-controls
        min-camera-orbit={product.mincameraorbit} 
        max-camera-orbit={product.maxcameraorbit}
        interaction-prompt="none">  
            <button slot="ar-button" className="ar-button">
                View in your space
            </button>
    </model-viewer>
)};

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!