简体   繁体   English

尝试 map 我的道具时,我收到无法读取未定义错误的“练习”属性

[英]I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props

Here's my "database" that I'm using,这是我正在使用的“数据库”,

index.js: index.js:

const workoutPlan = {
  ID: 1,
  Name: "My Workout Plan",
  workout: {
    day: "Monday",
    exercises: [
      {
        Name: "Bench Press",
        sets: [
          {
            set: 1,
            weight: 135,
            rep: 10
          },
          {
            set: 2,
            weight: 145,
            rep: 8
          }
        ]
      },

      {
        Name: "Deadlift",
        sets: [
          {
            set: 1,
            weight: 135,
            rep: 10
          },
          {
            set: 2,
            weight: 145,
            rep: 8
          }
        ]
      }
    ]
  }
};

Which then in my WorkoutPlan.js file, I'm passing the properties of the workoutPlan to my Workout.js child.然后在我的 WorkoutPlan.js 文件中,我将锻炼计划的属性传递给我的 Workout.js 孩子。

WorkoutPlan.js:锻炼计划.js:

import React from "react";
import Workout from "./Workout";
import axios from "axios";

class WorkoutPlan extends React.Component {
  constructor() {
    super();
    this.state = {
      workoutPlan: {}
    };
  }

  componentDidMount() {
    axios
      .get("/api/workout-plan")
      .then(response => {
        this.setState({ workoutPlan: response.data });
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    const { workoutPlan } = this.state;
    // const workoutPlan = this.state.workoutPlan;

    return (
      <div>
        <h1>{workoutPlan.Name}</h1>
        <Workout workout={workoutPlan.workout} />
      </div>
    );
  }
}

export default WorkoutPlan;

In my Workout.js file, I'm trying to only map the exercises property of WorkoutPlan.workout.exercises.在我的 Workout.js 文件中,我只尝试 map WorkoutPlan.workout.exercises 的练习属性。 However, when I run this code, I'm running into a TypeError: Cannot read property 'exercises' of undefined但是,当我运行此代码时,我遇到了 TypeError: Cannot read property 'exercises' of undefined

What am I doing wrong in this Child?我在这个孩子身上做错了什么?

Workout.js:锻炼.js:

import React from "react";
import Exercise from "./Exercise";

// const Workout = props => {
//   props.workout.exercises.map(exercises => {
//     return (
//       <div>
//         <h2>"Workout for {props.day}"</h2>
//         <Exercise exercises={exercises} />
//       </div>
//     );
//   });
// };

function Workout(props) {
  props.workout.exercises.map(exercises => {
    return (
      <div>
        <h2>"Workout for {props.day}"</h2>
        <p>{exercises.Name}</p>
        <Exercise exercises={exercises} />
      </div>
    );
  });
}

export default Workout;

I've commented out the const, as I'm told that they're both methods are interchangeable.我已经注释掉了 const,因为有人告诉我它们都是可以互换的方法。 But if someone could actually provide some insight on how to do it on both methods, I'd be so grateful.但是,如果有人真的可以提供一些关于如何在这两种方法上做到这一点的见解,我将不胜感激。 The error is specifically referring to该错误具体指

  props.workout.exercises.map(exercises => {

in my Workout.JS file在我的 Workout.JS 文件中

On the first render, the props.workout value passed to the Workout is undefined because looking in the parent component WorkoutPlan the default value for this.state.workoutPlan is {} so calling workoutPlan.workout will be `undefined.在第一次渲染时,传递给Workoutprops.workout值是undefined的,因为在父组件WorkoutPlan中查看this.state.workoutPlan的默认值是{} ,因此调用锻炼计划workoutPlan.workout将是“未定义的”。

There are two approaches, in the parent don't render this component if the value is undefined .有两种方法,如果值为undefined ,则在父级中不渲染此组件。 Or, in the child, handle the value of undefined .或者,在孩子中,处理undefined的值。

Handling in the parent:在父母中处理:

  render() {
    const { workout } = this.state.workoutPlan;

    return (
      <div>
        <h1>{workoutPlan.Name}</h1>
        {workout && <Workout workout={workout} />}
      </div>
    )
  }

Or, in the child:或者,在孩子身上:

function Workout(props) {
  // Early return if falsy value is passed (eg: undefined, null)
  if (!props.workout) return null;

  return props.workout.exercises.map(exercises => {
    return (
      <div>
        <h2>"Workout for {props.day}"</h2>
        <p>{exercises.Name}</p>
        <Exercise exercises={exercises} />
      </div>
    );
  });
}

Note, you also need to return the props.workout.exercises.map value.注意,您还需要return props.workout.exercises.map值。

暂无
暂无

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

相关问题 TypeError: Cannot read property 'map' of undefined 当我尝试 map 通过从父组件传递到子组件的道具时显示 - TypeError: Cannot read property 'map' of undefined is showing when I'm trying to map over the props passed from parent component to child component 我在我的 React 项目中收到此错误“TypeError: Cannot read property 'map' of undefined” - I'm getting this error “TypeError: Cannot read property 'map' of undefined” on my React project 尝试访问嵌入的Google地图时,为什么会出现无法读取未定义的属性“ getCenter”的问题? - Why am I getting Cannot read property 'getCenter' of undefined when trying to access my embed google map? 当我尝试将后端与 React 连接时,为什么它会返回“无法读取未定义的属性“地图”? - Why does it return `Cannot read property 'map' of undefined` when I'm trying to connect backend with React? 我试图 map 数据库中的数据并收到此错误。 TypeError:无法读取未定义的属性“地图” - I trying to map the data from the database and getting this error. TypeError: Cannot read property 'map' of undefined 尝试将 map 输出 state 并将其作为道具传递给组件时,“无法读取未定义的属性‘映射’” - “Cannot read property 'map' of undefined” when trying to map out state and pass it as props into component 在 JS 中迭代我的数组,但出现“无法读取未定义的属性‘长度’”错误 - Iterating on my array in JS but I'm getting a "Cannot read property 'length' of undefined" error 飞越图表时出现此错误:TypeError: Cannot read property 'lineWidth' of undefined - I'm getting this error when fly over the chart: TypeError: Cannot read property 'lineWidth' of undefined 我正在尝试从数据库中获取 map mydata 但我收到此错误。 TypeError:无法读取未定义的属性“地图” - I am trying to map mydata from the database but i am getting this error. TypeError: Cannot read property 'map' of undefined 我收到 TypeError:尝试在 React 的功能组件中使用道具时无法设置未定义的属性“道具”? - I'm getting TypeError: Cannot set property 'props' of undefined while trying to use props inside functional component in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM