简体   繁体   English

× 类型错误:无法读取未定义的属性(读取“名称”)

[英]× TypeError: Cannot read properties of undefined (reading 'name')

The problem is undefined of currentPackage.问题是 currentPackage 的未定义。 render() { const { hasAccounts, asideClassesFromConfig, disableScroll, htmlClassService, currentPackage, user, } = this.props;渲染(){ const { hasAccounts,asideClassesFromConfig,disableScroll,htmlClassService,currentPackage,用户,} = this.props; const isActive = checkStatus(user?.status); const isActive = checkStatus(user?.status);

const packageLogo = currentPackage.name && currentPackage.logo && (
  <div className="vk-inductee-button-wrapper">
    <div className="vk-inductee-button-image-wrapper">
      <img
        src={currentPackage.logo}
        className="vk-inductee-button-image"
        alt="inductee-img"
      />
    </div>
    <div className="vk-inductee-button-label">
      <span>{currentPackage.name}</span>
    </div>
  </div>
);

return (
  <>
    <button className="kt-aside-close" id="kt_aside_close_btn">
      <i className="la la-close" />
    </button>
    <div
      id="kt_aside"
      ref={this.asideOffCanvasRef}
      className={`kt-aside ${asideClassesFromConfig} kt-grid__item kt-grid kt-grid--desktop kt-grid--hor-desktop`}>
      <Brand />
      <div
        id="kt_aside_menu_wrapper"
        className="kt-aside-menu-wrapper kt-grid__item kt-grid__item--fluid">
        <AsideDropdown />
        {isActive ? (
          <Link to="/shop/my-packages">{packageLogo}</Link>
        ) : (
          packageLogo
        )}
        {disableScroll && <Menu htmlClassService={htmlClassService} />}
        {!disableScroll && (
          <PerfectScrollbar
            options={{ wheelSpeed: 2, wheelPropagation: false }}>
            <Menu htmlClassService={htmlClassService} />
          </PerfectScrollbar>
        )}
        {hasAccounts && <AsideFooter />}
      </div>
    </div>
  </>
);

} } } }

you might do something like你可能会做类似的事情

const packageLogo = currentPackage && currentPackage.name && currentPackage.logo && (
  <>
   //your code
  </>
)

the problem is that currentPackage is undefined, but you're checking if currentPackage.name is truthy.问题是currentPackage未定义,但您正在检查currentPackage.name是否为真。 Since the property name doesn't exist on undefined , you get that error.由于undefined上不存在属性name ,因此您会收到该错误。 You should just need to check that currentPackage itself is truthy before checking specific properties on it.在检查特定属性之前,您只需要检查currentPackage本身是否为真。

暂无
暂无

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

相关问题 TypeError:无法读取未定义的属性(读取“名称”); - TypeError: Cannot read properties of undefined (reading 'name'); TypeError:无法读取未定义的属性(读取“名称”) - TypeError: Cannot read properties of undefined (reading 'name') “TypeError:无法读取未定义的属性(读取'名称')” - "TypeError: Cannot read properties of undefined (reading 'name')" REACT JS - TypeError:无法读取未定义的属性(读取“名称”) - REACT JS - TypeError: Cannot read properties of undefined (reading 'name') TypeError:无法读取 js 中未定义(读取“名称”)的属性 - TypeError: Cannot read properties of undefined (reading 'name') in js Json TypeError:无法读取未定义的属性(读取“名称”) - Json TypeError: Cannot read properties of undefined (reading 'name') React TypeError:无法读取未定义的属性(读取“名称”) - React TypeError: Cannot read properties of undefined (reading 'name') 未捕获的类型错误:无法读取未定义的属性(读取“名称”)-ReactJS - Uncaught TypeError: Cannot read properties of undefined (reading 'name') - ReactJS 未捕获的类型错误:无法读取未定义的属性(读取“名称”)| 材质界面 - Uncaught TypeError: Cannot read properties of undefined (reading 'name') | Material UI 类型错误:无法读取未定义的属性(读取“createdTimestamp”) - TypeError: Cannot read properties of undefined (reading 'createdTimestamp')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM