【问题标题】:how to solve undefined error in react js when fetching data from API?从API获取数据时如何解决React js中的未定义错误?
【发布时间】:2019-01-24 05:16:18
【问题描述】:

收到类似 TypeError 登陆的错误。插槽未定义

<div className="notification" />
        </header>
        <main
          style={
            ready
              ? {}
              : {
                  height: "100vh",
                  overflow: "hidden"
                }
          }>
          <LandingLoader className={ready ? "fade-out" : ""} />
          <ul
            className="slots"
            style={{
              visibility: ready ? "visible" : "hidden"
            }}>
            {landing &&
              landing.Slots.map((slot,index) => {
                if (slot && !slot.removed) {
                  let fullWidth = "100%";
                  let maxWidth = 576;

                  let style = {};
                  style["width"] = fullWidth;
                  style["maxWidth"] = fullWidth;
                  style["height"] = slot.HeightPixel + "px";

                  return (
                    <li key={index} className="slots" style={style}>
                      <Slot slotItem={slot} slotIndex={index} />
                    </li>
                  );
                }
              })}
          </ul>
        </main>
      </div>

【问题讨论】:

    标签: json reactjs api


    【解决方案1】:

    您可以为插槽添加另一个检查,例如

    landing && landing.Slots && landing.Slots.map
    

    还请检查,是Slots 还是slots。可能是笔误

    【讨论】:

    • 是的,它只是错误类型,我也尝试将插槽作为插槽。我遇到了同样的错误
    • { "data":{ "id":1, "lastUpdatedOn":1543843081000, "createdOn":1543843081000, "pageUID":"SLLXHJ1", "languageTag":"en-us", "type":"Home", "name":"Home", "description":"Home page", "slots":[ {
    猜你喜欢
    • 2021-08-23
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    相关资源
    最近更新 更多