简体   繁体   English

“TypeError:无法解构'productDetails'的属性'product',因为它未定义”

[英]"TypeError: Cannot destructure property 'product' of 'productDetails' as it is undefined"

I am currently trying to display product data on the product screen.我目前正在尝试在产品屏幕上显示产品数据。 This is the error I'm getting in the console when opening a product details screen.这是我在打开产品详细信息屏幕时在控制台中遇到的错误。

TypeError: Cannot destructure property 'product' of 'productDetails' as it is undefined. TypeError:无法解构“productDetails”的属性“product”,因为它未定义。

I'm not sure why productDetails is undefined.我不确定为什么 productDetails 未定义。 It should contain product details data.它应该包含产品详细信息数据。

Here is my code.这是我的代码。

ProductScreen.js ProductScreen.js

import React, { useEffect } from 'react';
import { useParams } from "react-router-dom";
import {Link} from "react-router-dom";
import { useSelector, useDispatch } from 'react-redux';
import { detailsProduct } from "../actions/productActions"

function ProductScreen() {
    const { id } = useParams();
    
    const productDetails = useSelector(state => state.productDetails);
    const {product, loading, error} = productDetails;
    const dispatch = useDispatch; 
    console.log("something");

    useEffect(() => {
        dispatch(detailsProduct(id));
        return () => {
            //
        };
    }, []);

    return <div>
        <div className="back-to-results">
            <Link to="/">Back to results</Link>
        </div>
        {loading ? <div>Loading...</div> :
        error ? <div>{error}</div> :
        (
            <div className="details">
            <div className="details-image">
                <img src={product.image} alt="product" ></img>
            </div>
            <div className="details-info">
                <ul>
                    <li>
                        <h4>{product.name}</h4>
                    </li>
                    <li>
                        {product.rating} Stars ({product.numReviews} Reviews)
                    </li>
                    <li>
                        {product.price}
                    </li>
                    <li>
                        Description:
                        <div>
                            {product.description}
                        </div>
                    </li>
                </ul>
            </div>
            <div className="details-action">
                <ul>
                    <li>
                        Price: <b>${product.price}</b>
                    </li>
                    <li>
                        Status: {product.status}
                    </li>
                    <li>
                        Qty: <select>
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                        </select>
                    </li>
                    <li>
                        <button className="button">Add to Cart</button>
                    </li>
                </ul>
            </div>
        </div>
        )
}
    </div>
}
export default ProductScreen;

productActions.js productActions.js

import { PRODUCT_LIST_FAIL, PRODUCT_LIST_REQUEST, PRODUCT_LIST_SUCCESS, PRODUCT_DETAILS_REQUEST, PRODUCT_DETAILS_SUCCESS, PRODUCT_DETAILS_FAIL } from "../constants/productconstants.js";
import axios from "axios";

const listProducts = () => async (dispatch) => {

    try {
        dispatch({type: PRODUCT_LIST_REQUEST});
        const {data} = await axios.get("/api/products");
        dispatch({type: PRODUCT_LIST_SUCCESS, payload: data});
    }
    catch (error) {
        dispatch({type: PRODUCT_LIST_FAIL, payload:error.message});
    }
}

const detailsProduct = (productId) => async (dispatch) => {
    try {
        dispatch({type: PRODUCT_DETAILS_REQUEST, payload: productId});
        const {data} = await axios.get("/api/products/" + productId);
        dispatch({type: PRODUCT_DETAILS_SUCCESS, payload: data});
    }
    catch (error) {
        dispatch({type: PRODUCT_DETAILS_FAIL, payload: error.message});
    }
}

export {listProducts, detailsProduct};

Initially when the page is rendered productDetails state should be empty or undefined although you have dispatched detailsProduct in useEffect but it will take some time to get data from API request that's why you are getting the error.最初,当页面呈现时 productDetails state 应该为空或未定义,尽管您已在 useEffect 中调度了 detailsProduct 但需要一些时间才能从 API 请求中获取数据,这就是您收到错误的原因。 You can use productDetails without destructure您可以使用 productDetails 而无需解构

productDetails?.loading
productDetails?.product?.name

the question mark between productDetails and loading is Optional Chaining you can read more: Optional Chaining productDetails 和 loading 之间的问号是 Optional Chaining 你可以阅读更多: Optional Chaining

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法解构“未定义”的属性“位置”,因为它未定义 - TypeError: Cannot destructure property 'position' of 'undefined' as it is undefined TypeError:无法解构“产品”的属性“图像”,因为它是 null - TypeError: Cannot destructure property 'image' of 'product' as it is null TypeError:无法解构“对象(...)(...)”的属性“用户”,因为它未定义 - TypeError: Cannot destructure property 'user' of 'Object(...)(...)' as it is undefined × TypeError:无法解构'Object(...)(...)'的属性'currentUser',因为它是未定义的 - × TypeError: Cannot destructure property 'currentUser' of 'Object(...)(...)' as it is undefined 类型错误:无法解构“项目”的属性“名称”,因为它未定义 - TypeError: Cannot destructure property 'name' of 'item' as it is undefined TypeError:无法解构“未定义”或“空”的属性“stat” - TypeError: Cannot destructure property `stat` of 'undefined' or 'null' TypeError:无法解构“orderPay”的属性“加载”,因为它未定义 - TypeError: Cannot destructure property 'loading' of 'orderPay' as it is undefined TypeError:无法解构“未定义”或“空”的属性“queryResult” - TypeError: Cannot destructure property `queryResult` of 'undefined' or 'null' TypeError:无法解构“userUpdate”的属性“加载”,因为它未定义 - TypeError: Cannot destructure property 'loading' of 'userUpdate' as it is undefined TypeError:无法解构“未定义”的属性“list2” - TypeError: Cannot destructure property 'list2' of 'undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM