简体   繁体   English

JavaScript TypeError,无法读取未定义的属性,但函数已定义。 我究竟做错了什么?

[英]JavaScript TypeError, Cannot read property of undefined, but the function is defined. What am I doing wrong?

I keep getting the error: "TypeError: Cannot read property 'push' of undefined" 我不断收到错误消息:“ TypeError:无法读取未定义的属性'push'”

The error is in this line: "this.courseName.push(dish);" 错误在以下行中:“ this.courseName.push(dish);”

Here is the code: 这是代码:

let menu = {
    _courses: {
      _appetizers:[] ,
      _mains:[],
      _desserts: []
    },

    addDishToCourse(courseName, dishName, dishPrice){
        const dish = { name: this.dishName, price: this.dishPrice };

        // ERROR HERE: **this._courses[courseName].push(dish);**
    },
};

I have been stuck with this error for hours. 我已经被这个错误困扰了几个小时。 Is there something I'm not seeing? 有没有我看不到的东西吗?

Sorry if there is a simple solution to this. 抱歉,如果有一个简单的解决方案。 I'm relatively new to JavaScript. 我对JavaScript比较陌生。

Edit: 编辑:

I was indeed calling the key incorrectly. 我确实是错误地调用了密钥。 I am now calling the function as: 我现在将该函数称为:

  menu.addDishToCourse("_appetizers", "random", 10);
  console.log(menu._courses._appetizers);

When I log this in compiler it returns: 当我在编译器中登录时,它返回:

    [ { name: undefined, price: undefined } ]

It seems like the function isn't getting called. 似乎没有调用该函数。 Am I approaching this fundamentally incorrectly? 我是否从根本上错误地解决了这个问题?

It is the scope that is the issue. 问题在于scope

this is the object itself (the menu in this case) and has _courses and addDishToCourse as its properties. thisobject本身(在这种情况下为菜单),并具有_coursesaddDishToCourse作为其属性。

Inside the addDishToCourse this is looking at the object for the dishName and dishPrice , but they are scoped to the function itself (from the parameters). addDishToCourse内部, this是在查看objectdishNamedishPrice ,但它们的作用域dishPrice于函数本身(通过参数)。

So the correct code would be like so: 因此正确的代码如下所示:

let menu = {
        _courses: {
            _appetizers: [],
            _mains: [],
            _desserts:[]
        },

        addDishToCourse(courseName, dishName, dishPrice) {
            const dish = { name: dishName, price: dishPrice };

            this._courses[courseName].push(dish);
        }
    }    

menu.addDish("_mains", "Pizza", "10");

Your first problem is that you were passing a wrong courseName to the function, that's why you get the error. 您的第一个问题是,您向函数传递了错误的courseName ,这就是为什么会出现错误。

And your second problem is that you are assigning wrong values, to object to push in your courses array when you used { name: this.dishName, price: this.dishPrice } . 第二个问题是使用{ name: this.dishName, price: this.dishPrice }时,您分配了错误的值来反对将您的课程数组推入。

In fact when you use this.dishName you were trying to get dishName from your menu object that's why it's undefined , you just need to use the parameters dishName and dishPrice you are passing arguments to the function. 实际上,当您使用this.dishName您试图从menu对象中获取dishName ,这就是undefined的原因,您只需要使用参数dishNamedishPrice dishNamedishPrice可以将参数传递给该函数。

    const dish = {
      name: dishName,
      price: dishPrice
    };

    this._courses[courseName].push(dish);

This is a working Demo: 这是一个有效的演示:

 let menu = { _courses: { _appetizers: [], _mains: [], _desserts: [] }, addDishToCourse(courseName, dishName, dishPrice) { const dish = { name: dishName, price: dishPrice }; this._courses[courseName].push(dish); }, }; menu.addDishToCourse("_appetizers", "random", 10); console.log(menu._courses._appetizers); 

暂无
暂无

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

相关问题 Reducer无法读取未定义的属性“照片”? 我究竟做错了什么? - Reducer cannot read property 'photos' of undefined? What am I doing wrong? 我究竟做错了什么? TypeError:这是未定义的 - What am i doing wrong? TypeError: this is undefined 我做错了什么(javascript对象)未捕获的类型错误:无法读取未定义的属性“0” - what did I do wrong (javascript object) Uncaught TypeError: Cannot read property '0' of undefined Discord.js - 类型错误:无法读取未定义的属性“声音”。 有人可以告诉我我做错了什么吗? - Discord.js - TypeError: Cannot read property 'voice' of undefined. Can someone tell me what I'm doing wrong? 无法弄清楚我做错了什么。 未处理的拒绝(TypeError):无法读取未定义的属性“检查” - Cant figure out what Im doing wrong. Unhandled Rejection (TypeError): Cannot read property 'inspection' of undefined AngularJS:服务错误,服务未定义。 我究竟做错了什么? - AngularJS: Services error, service not defined. What am I doing wrong? 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property 'initializeApp' of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 为什么在定义的数组上收到“ ERROR TypeError:无法读取未定义的属性'length'的信息”? - Why am I receiving “ERROR TypeError: Cannot read property 'length' of undefined” on a defined array? TypeError:无法读取未定义的属性“ length”(由功能参数定义) - TypeError: cannot read property 'length' of undefined (defined by function parameter) TypeError:对象不是函数-我在做什么错? - TypeError: object is not a function - what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM