简体   繁体   English

类型错误:无法将 undefined 或 null 转换为对象

[英]TypeError: Cannot convert undefined or null to object

I'm a beginner here and i was working with some react course from udemy and while everything was working perfect and fine and all my files are double checked with the instructor by myself but at this point it is throwing an error which i'm unable to figure out.我是这里的初学者,我正在学习 udemy 的一些反应课程,虽然一切都运行良好,我的所有文件都由我自己与讲师进行了双重检查,但此时它抛出了一个我无法做到的错误弄清楚。

the error is thrown as shown below.如下所示抛出错误。

 6 |    const transformedIngredients = Object.keys(props.ingredients)
   7 |    .map(igKey => {
   8 |        return [...Array(props.ingredients[igKey])].map((_, i) => {
>  9 |           return <BurgerIngredient key={igKey + i} type={igKey} />;
     |                 ^  
  11 |        });
  12 |    });

burger.js汉堡.js

import React from 'react';
import classes from './Burger.css';
import BurgerIngredient from './BurgerIngredient/BurgerIngredient';

const burger = (props) => {
    const transformedIngredients = Object.keys(props.ingredients)
    .map(igKey => {
        return [...Array(props.ingredients[igKey])].map((_, i) => {
           return <BurgerIngredient key={igKey + i} type={igKey} />;

        });
    });
    return (
    <div className ={classes.Burger}>
        <BurgerIngredient type="bread-top" />
       {transformedIngredients}
        <BurgerIngredient type="bread-bottom" />
    </div>
    );
};

export default burger; 

the code is same as the instructor but throwing above error.代码与讲师相同,但抛出上述错误。

other two files are pasted below其他两个文件粘贴在下面

burgerbuilder.js汉堡建造者.js

import React, { Component } from 'react';
import Auxy from '../../Hoc/Auxy'; 
import Burger from '../../components/Burger/Burger';

class BurgerBuilder extends Component {

    state = {
        ingredients: {
            salad: 1,
            bacon: 1,
            meat: 1,
            cheese: 2

        }
    }
    render() {
        return ( 
            <Auxy>
                 <Burger />
                <div>Build Controls </div>

            </Auxy>
        );
    }
}
export default BurgerBuilder;

burgeringredient.js burgeringredient.js

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import classes from './BurgerIngredient.css';

class BurgerIngredient extends Component {
    render () {

        let ingredient = null;
        switch (this.props.type){
        case ('bread-bottom'):
        ingredient = <div className={classes.BreadBottom}></div>;
        break;
        case ('bread-top'):
        ingredient = (
            <div className={classes.BreadTop}>
            <div className={classes.Seeds1}></div>
            <div className={classes.Seeds2}></div>
            </div>
        );
        break;
        case ('meat'):
        ingredient =<div className={classes.Meat}></div>;
        break;
        case ('cheese'):
        ingredient =<div className={classes.Cheese}></div>;
        break;
        case ('bacon'):
        ingredient =<div className={classes.Bacon}></div>;
        break;
        case ('salad'):
        ingredient =<div className={classes.Salad}></div>;
        break;
        default:
        ingredient =null;
    }
    return ingredient;

    }
} 

BurgerIngredient.propTypes={
    type: PropTypes.string.isRequired
};

export default BurgerIngredient;

Had the same issue! 有同样的问题!

in burgerbuilder.js make sure to return pass ingredients into the burger component: 在burgerbuilder.js中,确保将传递的成分返回到burger组件中:

             <Burger ingredients={this.state.ingredients} />

For people who get this error, also check if you have declared your state in BurgerIngredients.js instead of BurgerBuilder.js对于收到此错误的人,还要检查您是否在 BurgerIngredients.js 而不是 BurgerBuilder.js 中声明了您的状态

Thank me later.晚点再谢我。

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

相关问题 类型错误:无法将未定义或空值转换为对象 javascript - TypeError : Cannot convert undefined or null to object javascript TypeError:无法在反应中将未定义或 null 转换为 object - TypeError: Cannot convert undefined or null to object in react 错误TypeError:无法将未定义或null转换为对象 - error TypeError: Cannot convert undefined or null to object Javascript:TypeError:无法将未定义或null转换为对象 - Javascript: TypeError: Cannot convert undefined or null to object 未捕获的TypeError:无法将未定义或null转换为对象 - Uncaught TypeError: Cannot convert undefined or null to object Sequelize TypeError:无法将未定义或空值转换为对象 - Sequelize TypeError: Cannot convert undefined or null to object TypeError:无法将undefined或null转换为object(引用Object.keys) - TypeError: Cannot convert undefined or null to object (referring to Object.keys) TypeError: 删除 object 的键时,无法将 undefined 或 null 转换为 object - TypeError: Cannot convert undefined or null to object when deleting the key of object 对于给出 TypeError 的空对象:无法将 undefined 或 null 转换为对象 - For empty object giving the TypeError: Cannot convert undefined or null to object MERN Stack - TypeError:无法将undefined或null转换为object - MERN Stack - TypeError: Cannot convert undefined or null to object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM