NodeJS 未捕获的类型错误:无法读取未定义的属性(正在阅读“params”)

qij5mzcb  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(125)

请告诉我怎么解决这个问题

product.action.js
import axios from '../helpers/axios';

export const getProductsBySlug = (slug) => {
    
    return async dispatch =>{
        const res = await axios.get(`/products/${slug}`);
        console.log(res);
    }
}
productListPage
[![enter image description here](https://i.stack.imgur.com/cxmbI.png)](https://i.stack.imgur.com/cxmbI.png)

import React, { useEffect } from "react";
import { useDispatch } from "react-redux";
import { getProductsBySlug } from "../../actions";
import Layout from "../../components/Layout";

/**
 * @author
 * @function ProductListPage
 **/

export const ProductListPage = (props) => {
  const dispatch = useDispatch();

  useEffect(() => {
    const { match } = props;
    dispatch(getProductsBySlug(match.params.slug));
  }, []);

  return <Layout>Product List Page</Layout>;
};

我不明白是什么问题,所以我收到一条错误消息- TypeError:无法读取未定义的属性(正在阅读“params”)

7hiiyaii

7hiiyaii1#

试着把额外的检查和调度时,鼻涕虫成为可用

export const ProductListPage = (props) => {

  const dispatch = useDispatch();

  const { match } = props;

  const slug = match?.params?.slug

  useEffect(() => {

    if (slug) {
      dispatch(getProductsBySlug(slug));
    }   
    
  }, [slug]);

  return <Layout>Product List Page</Layout>;
};

希望能有所帮助

相关问题