简体   繁体   English

Uncaught typeError:无法读取未定义的属性-对象

[英]Uncaught typeError: Cannot read property of undefined - object

I don't think my script is identifying an object, and I'm not sure why. 我不认为我的脚本正在标识对象,而且我不确定为什么。

I start with the following data structure: 我从以下数据结构开始:

var state;
var init = function() {
    state = {
        runInfo: { 
            price: null,
            containers: null,
            servings: null
        },
        formula: [],
        totalsServing: [],
        totalsBottle: [],
        totalsRun: []
    };
};

And then add objects to the 'Formula' array: 然后将对象添加到“公式”数组中:

var addIngredient = function(name, amount, carrier, usdperkilo, origin, packSize) {
        var ingredient = {
            name: name,
            amount: amount,
            carrier: carrier,
            usdperkilo: usdperkilo,
            origin: origin,
            packSize: packSize
        };

        state.formula.push(ingredient);
    };

This all works fine. 这一切都很好。 However, when I create another function to manipulate the objects in the "Formula" array I get an "Uncaught TypeError: Cannot define property 'carrier' of undefined" using this function: 但是,当我创建另一个函数来操纵“ Formula”数组中的对象时,使用此函数会收到“ Uncaught TypeError:无法定义未定义的属性'carrier'”:

addSection3Row = function(ingredient) {
        var ingredient = state.formula[ingredient].name;
        var carrier = (state.formula[ingredient].carrier/100)*(state.formula[ingredient].amount/(1-(state.formula[ingredient].carrier/100)));
        var amount = state.formula[ingredient].amount;

      var row = {
            ingredient: ingredient,
            carrier: carrier,
            amount: amount,
            totalAmount: carrier + amount
        };

        state.totalsServing.push(row);
    };

I've looked at other questions w/ this error message the issue usually seems to be that javascript hasn't defined the variable yet. 我已经看过其他问题,带有此错误消息,该问题通常似乎是javascript尚未定义变量。

I suspect this is the case for me too, but I can't see why. 我怀疑这也是我的情况,但我不明白为什么。

I run the following code at the bottom of my page, beneath the html: 我在页面底部的html下面运行以下代码:

init();
addIngredient("St. John's Wort", 500, 4, 25, true, 25);
addIngredient("5-HTP", 100, 2, 165, true, 25);
addSection3Row(0);
console.log(state); 

And when I run the following code in the console: 当我在控制台中运行以下代码时:

(state.formula[ingredient].carrier/100)*(state.formula[ingredient].amount/(1-(state.formula[ingredient].carrier/100)))

It returns the correct value. 它返回正确的值。 So I know there's something in my script that prohibits js from accessing the object but I'm not sure why. 所以我知道我的脚本中有一些东西禁止js访问对象,但是我不确定为什么。

In addSection3Row function you override the ingredient in first row. 在addSection3Row函数中,您将覆盖第一行中的成分。

addSection3Row = function(ingredient) { //ingredient is number

        var ingredient = state.formula[ingredient].name;//ingredient is string
        var carrier = (state.formula[ingredient].carrier/100)*(state.formula[ingredient].amount/(1-(state.formula[ingredient].carrier/100)));
        var amount = state.formula[ingredient].amount;

      var row = {
            ingredient: ingredient,
            carrier: carrier,
            amount: amount,
            totalAmount: carrier + amount
        };

        state.totalsServing.push(row);
    };

暂无
暂无

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

相关问题 未捕获的类型错误:无法读取未定义的属性“对象” - Uncaught TypeError: Cannot read property 'object' of undefined 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的类型错误:无法读取 Object.onLoad 处未定义的属性“setAttribute” - Uncaught TypeError: Cannot read property 'setAttribute' of undefined at Object.onLoad 未捕获的类型错误:无法读取未定义对象的属性“indexOf” - Uncaught TypeError: Cannot read property 'indexOf' of undefined with Object 未捕获的TypeError:无法读取对象中未定义的属性“样式” - Uncaught TypeError: Cannot read property 'style' of undefined in object Uncaught TypeError:无法读取未定义的属性“ canvas”-Javascript对象 - Uncaught TypeError: Cannot read property 'canvas' of undefined - Javascript Object js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball - js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball 未捕获的类型错误:无法读取 AJAX object 上未定义的属性“映射” - Uncaught TypeError: Cannot read property 'map' of undefined on AJAX object 未捕获的TypeError:无法读取未定义的属性“数量” - Uncaught TypeError: Cannot read property 'quantity' of undefined 未捕获的TypeError:无法读取未定义的属性'fromJSON' - Uncaught TypeError: Cannot read property 'fromJSON' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM