简体   繁体   English

即使对象已定义,也无法读取“未定义”的属性! [反应]

[英]Cannot read property of “undefined” even though the object IS defined! [React]

I am trying to generate the markup for a dropdown menu that will allow users to change the language/locale of the site. 我正在尝试为下拉菜单生成标记,该标记将允许用户更改网站的语言/区域设置。

But my component will not render because the browser is telling me the object (language) I'm trying to pull country names (locales) off of is undefined. 但是我的组件无法渲染,因为浏览器告诉我我试图提取国家名称(语言环境)的对象(语言)是未定义的。

What I don't understand is that when I console log that same language object and the key that I'm passing to it (selectedLang) in the lines above, they both print out just fine. 我不明白的是,当我在上面的行中控制台记录该语言对象和要传递给它的键(selectedLang)时,它们都可以正常打印。

Here is my code: 这是我的代码:

compileAvailableLocales() {
        let locales = availableLangs;
        let selectedLang = this.props.lang;
        console.log("selectedLang: ", selectedLang, typeof selectedLang);
        console.log("availableLangs: ", availableLangs);
        console.log("language: ", language);

        let markup = locales.map( (loc) => {
            let readableName = language[ selectedLang ].navigation.locales[ loc ];

            return (
                <li
                  key={ loc }
                  value={ loc }
                  onMouseDown={ this.handleLangChange }>
                    { readableName }
                </li>
            );
        });

        return markup;
    }

I think the problem may be the way that I am importing the language object from my essential_lang.js file. 我认为问题可能是我从essential_lang.js文件导入语言对象的方式。

I'm importing into the index.jsx file (see code above) with this line: 我使用以下行将其导入index.jsx文件(请参见上面的代码):

import { language } from '../../constants/essential_lang'

And here is some of the code from that file: 这是该文件中的一些代码:

   export const language = {
    'de_de': {
        footer: {
            links: [
                {
                    title: 'KUNDENSERVICE',
                    endpoint: 'mailto:kundenservice@spinmaster.com'
                },
                {
                    title: 'Datenschutz',
                    endpoint: '/de_de/privacy'
                },
                {
                    title: 'Impressum und Allgemeine Geschäftsbedingungen',
                    endpoint: '/de_de/terms'
                }
            ],
            copyright: 'Spin Master Ltd. All Rights Reserved.'
        },
        fourohfour: {
            heading: 'Oh Noes!',
            message: 'This page doesn\'t exist.',
            gohome: 'Back Home'
        },
        loading: "Loading",
        navigation: {
            menuitems: [
                {
                    title: 'Videos',
                    endpoint: '/de_de/videos/'
                },
                {
                    title: 'Colleggtibles',
                    endpoint: '/de_de/colleggtibles'
                },
                {
                    title: 'Produkte',
                    endpoint: '/de_de/toys/'
                },
                {
                    title: 'Hilfe/Häufig gestellte Fragen',
                    endpoint: 'https://spinmastersupport.helpshift.com/a/hatchimals/?p=web&l=de'
                },
                {
                    title: 'Eltern',
                    endpoint: '/de_de/parents/'
                },
                {
                    title: 'Spiele',
                    endpoint: '/de_de/activities/'
                },
                {
                    title: 'Wo erhältlich',
                    endpoint: '/de_de/where-to-buy/'
                }
            ],
            locales: {
                de_de: 'Deutschland',
                en_au: 'Australia',
                en_ca: 'Canada',
                en_uk: 'United Kingdom',
                en_us: 'United States',
                es_es: 'España',
                es_mx: 'México',
                fr_fr: 'France',
                it_it: 'Italia',
                nl_nl: 'Dutch',
                tr_tr: 'Türkiye'
            }
        }
    },
    'en_au': { ...

Here is output for the console.logs: 这是console.logs的输出:

Is it as simple as returning a string? 它像返回字符串一样简单吗?

return "
            <li
              key={ loc }
              value={ loc }
              onMouseDown={ this.handleLangChange }>
                { readableName }
            </li>
        ";

Difficult without seeing all the code. 很难看到所有代码。

暂无
暂无

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

相关问题 “看不懂属性&#39; <propName> &#39;未定义&#39;,即使它是定义的 - “Cannot read property of '<propName>' of undefined” even though it's defined 未捕获的TypeError:无法读取未定义的属性“ 1”(即使已定义) - Uncaught TypeError: Cannot read property '1' of undefined (even though it is defined) TypeError:无法读取未定义的属性“映射”......即使属性已定义? - TypeError: Cannot read property 'map' of undefined...even though property is defined? Angular:无法读取未定义的属性“forEach” - 问题(即使应该定义) - Angular: Cannot read property 'forEach' of undefined - Issue (even though should be defined) “无法读取未定义的属性‘setState’”,即使它已绑定 - "Cannot read property 'setState' of undefined" even though it is bound TypeError:无法读取未定义的属性“状态”,即使已经绑定 - TypeError: Cannot read property 'state' of undefined, even though already binded “无法读取未定义的属性”,即使存在数据(React/JSX) - "Cannot read properties of undefined," even though data is there (React/JSX) 即使使用箭头函数,React 也无法读取 .then() 中未定义的属性 setState - React Cannot read property setState of undefined in .then() even using arrow function 在定义所有属性时,无法读取未定义的属性&#39;bind&#39; - Cannot read property 'bind' of undefined React when all properties are defined 反应:无法读取未定义的属性“时间”虽然“时间”已定义? - React: Cannot read property 'times' of undefined Although “times” is defined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM