简体   繁体   English

未捕获的类型错误:无法读取未定义的属性(读取“长度”)

[英]Uncaught TypeError: Cannot read properties of undefined (reading 'length')

I am creating an eCommerce website and getting the error in the title.我正在创建一个电子商务网站并在标题中出现错误。

This is my Navbar element, where I'm using state.length :这是我的 Navbar 元素,我在其中使用state.length

import React from "react";
import { useSelector } from "react-redux";
import { NavLink } from "react-router-dom";

const Navbar = () => {
  const state = useSelector((state) => state.handleCart);
  return (
    <div>
      <nav className="navbar navbar-expand-lg bg-white py-3 shadow-sm">
        <div className="container">
          <NavLink className="navbar-brand fw-bold fs-5" to="/">
            Some Generic Shop
          </NavLink>
          <button
            className="navbar-toggler"
            type="button"
            data-bs-toggle="collapse"
            data-bs-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent"
            aria-expanded="false"
            aria-label="Toggle navigation"
          >
            <span className="navbar-toggler-icon"></span>
          </button>
          <div className="collapse navbar-collapse" id="navbarSupportedContent">
            <ul className="navbar-nav mx-auto mb-2 mb-lg-0">
              <li className="nav-item">
                <NavLink className="nav-link active" aria-current="page" to={`/`}>
                  Home
                </NavLink>
              </li>
              <li className="nav-item">
                <NavLink className="nav-link" to={`/products`}>
                  Products
                </NavLink>
              </li>
              <li className="nav-item">
                <NavLink className="nav-link" to={`/about`}>
                  About
                </NavLink>
              </li>
              <li className="nav-item">
                <NavLink className="nav-link" to={`/contact`}>
                  Contact us!
                </NavLink>
              </li>
            </ul>
            <div className="buttons my-3 mx-1">
              <NavLink to="/login" className="btn btn-outline-dark">
                <i className="fa fa-sign-in me-1"></i> Login
              </NavLink>
            </div>
            <div className="buttons my-3 mx-1">
              <NavLink to="/register" className="btn btn-outline-dark">
                <i className="fa fa-user-plus me-1"></i> Sign up
              </NavLink>
            </div>
            <div className="buttons my-3 mx-1">
              <NavLink to="/cart" className="btn btn-outline-dark">
                <i className="fa fa-shopping-cart me-1"></i> Cart({state.length})
              </NavLink>
            </div>
          </div>
        </div>
      </nav>
    </div>
  );
};

export default Navbar;

This is my handleCart.js这是我的 handleCart.js

const cart=[];

const handleCart = (state = cart, action) => {
    const product = action.payload;

    switch(action.type) {
        case "ADDITEM":
            //check if product exists
        const exist_1 = state.find((x) => x.id === product.id);

        if(exist_1){
            //increase quantity
            return state.map((x)=>
            x.id === product.id ? {...x, qty: x.qty + 1}: x
            );
        } else {
            const product = action.payload;
            return[
                ...state,
                {
                    ...product,
                    qty: 1,
                }
            ]
        }
        break;

        case "DELETEITEM":

        const exist_2 = state.find((x) => x.id === product.id);
        if(exist_2.qty === 1){
            return state.filter((x)=> x.id !==exist_2.id);
        }else{
            return state.map((x)=>
                x.id === product.id ? {...x, qty: x.qty-1} : x
            );
        }
        break;

        default: 
            return state;
            break;

    }
        

       
}

and the index.js和 index.js

import handleCart from "./handleCart";
import { combineReducers } from "redux";

const rootReducers = combineReducers({
    handleCart,
})

export default rootReducers;

I'm completely new to React, so let me know what else you need me to show.我是 React 的新手,所以让我知道您还需要我展示什么。 I needed the Cart() in the navbar to show the number of items added.我需要导航栏中的Cart()来显示添加的项目数。 But it just returns this error.但它只是返回这个错误。 It works whenever I remove "state.length".每当我删除“state.length”时它都会起作用。

When I added state?.length , some of my app started to render, but the cart doesn't work still, and every time I click on add to cart, this error shows up:当我添加state?.length时,我的一些应用程序开始呈现,但购物车仍然无法正常工作,每次我点击添加到购物车时,都会出现此错误:

Store does not have a valid reducer.商店没有有效的减速器。 Make sure the argument passed to combineReducers is an object whose values are reducers.确保传递给 combineReducers 的参数是 object,其值为 reducer。

Not sure why the formatting error, I hope you guys get it!不知道为什么格式错误,我希望你们能明白!

It means when you call {{state.length}},it didn't exsit yet.这意味着当你调用 {{state.length}} 时,它还没有存在。 To fix this, you can set code to this:要解决此问题,您可以将代码设置为:

              <NavLink to="/cart" className="btn btn-outline-dark">
                <i className="fa fa-shopping-cart me-1"></i>  
{state.length?Cart({state.length}):""}
              </NavLink>

Means: When state.length is defined, then call Cart({state.length}) or do nothing.意思是:当 state.length 被定义时,然后调用 Cart({state.length}) 或什么都不做。

暂无
暂无

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

相关问题 未捕获的类型错误:无法在 GoogleMapReact 读取未定义的属性(读取“长度”) - Uncaught TypeError: Cannot read properties of undefined (reading 'length') at GoogleMapReact 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 类型错误:无法读取未定义的属性(读取“长度”) - TypeError: Cannot read properties of undefined (reading 'length') 未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”) - Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter') Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') Uncaught TypeError: Cannot read properties of undefined (reading &#39;length&#39;) 。 Jquery中的自动建议 - Uncaught TypeError: Cannot read properties of undefined (reading 'length') . auto-suggestion in Jquery 如何修复此错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“长度”) - how fix this error:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM