简体   繁体   English

Alpine JS - VM11705:16 Uncaught TypeError: Cannot read properties of undefined (reading 'title') 错误问题

[英]Alpine JS - VM11705:16 Uncaught TypeError: Cannot read properties of undefined (reading 'title') Error Issue

I made a widget using alpine js however, getting an error like and finding solution to my research.然而,我使用 alpine js 制作了一个小部件,遇到了类似的错误并为我的研究找到了解决方案。 What do you think is the error in my code?您认为我的代码中的错误是什么? When I pull the data, the header part is said to be incorrect.当我拉取数据时,据说header部分不正确。 But I can't see a problem.但我看不出有什么问题。 Or I couldn't figure it out.或者我想不通。

[Errors i see in console][1] [1]: https://i.stack.imgur.com/ofmW1.png [我在控制台中看到的错误][1] [1]:https://i.stack.imgur.com/ofmW1.png

<div class="text-base w-56 px-5 rounded-full overflow-hidden" x-data="{
                   textArray: [],
                text: '',
                textIndex: 0,
                charIndex: 0,
                pauseEnd: 2750,
                cursorSpeed: 420,
                pauseStart: 20,
                typeSpeed: 50,
                direction: 'forward'
             }" x-init="(() => {

                fetch('/wp-json/wp/v2/pages/219/?_fields=acf.positions&acf_format=standard')
                  .then(response => response.json())
                  .then(data => textArray = data.acf.positions  );



                let typingInterval = setInterval(startTyping, $data.typeSpeed);



             function startTyping(){
                let current = $data.textArray[$data.textIndex].title;
                if($data.charIndex > current.length){
                     $data.direction = 'backward';
                     clearInterval(typingInterval);
                     setTimeout(function(){
                        typingInterval = setInterval(startTyping, $data.typeSpeed);
                     }, $data.pauseEnd);
                }

                $data.text = current.substring(0, $data.charIndex);
                if($data.direction == 'forward'){
                    $data.charIndex += 1;
                 } else {
                    if($data.charIndex == 0){
                        $data.direction = 'forward';
                        clearInterval(typingInterval);
                        setTimeout(function(){

                            $data.textIndex += 1;
                            if($data.textIndex >= $data.textArray.length){
                                $data.textIndex = 0;
                            }

                            typingInterval = setInterval(startTyping, $data.typeSpeed);
                        }, $data.pauseStart);
                    }
                    $data.charIndex -= 1;
                 }

             }

             setInterval(function(){
                if($refs.cursor.classList.contains('hidden')){
                    $refs.cursor.classList.remove('hidden');
                } else {
                    $refs.cursor.classList.add('hidden');
                }
             }, $data.cursorSpeed);
         })()">


            <span x-text="text"></span>
            <span class="opacity-70" x-ref="cursor">|</span>
        </div>

Thanks in advance for your suggestions and help.提前感谢您的建议和帮助。

It's complaining that $data.textArray[$data.textIndex] is undefined, and you're trying to read .title from it.它抱怨$data.textArray[$data.textIndex]未定义,而您正试图从中读取.title

when you first load your widget you do a fetch to populate textArray , but until that returns your textArray is empty so when you're trying to call let current = $data.textArray[$data.textIndex].title it's returning the error当你第一次加载你的小部件时,你会做一个 fetch 来填充textArray ,但是在返回之前你的textArray是空的,所以当你试图调用let current = $data.textArray[$data.textIndex].title时它会返回错误

You basically need to ensure textArray has data before you try to access it:在尝试访问之前,您基本上需要确保 textArray 有数据:

  • you could move everything into your .then(data => textArray = data.acf.positions );您可以将所有内容移动到您的.then(data => textArray = data.acf.positions ); so it's only started when the fetch has been called.所以它只有在调用 fetch 时才开始。
  • or you could put a simple if (textArray.length === 0) return;或者你可以放一个简单的if (textArray.length === 0) return; line at the start of your startTyping function so it doesn't try to do anything until textArray is populated startTyping function 开头的行,因此在填充 textArray 之前它不会尝试做任何事情

might not be your only issue, but this is the cause of the error you've posted可能不是您唯一的问题,但这是您发布的错误的原因

暂无
暂无

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

相关问题 Alpine JS - 错误:未捕获类型错误:无法读取未定义的属性(读取'_x_refs') - Alpine JS - Error : Uncaught TypeError: Cannot read properties of undefined (reading '_x_refs') 我如何解决 javascript 错误:coin.js:16 Uncaught TypeError: Cannot read properties of undefined (reading 'inr') - how i can resolve javascript error :coin.js:16 Uncaught TypeError: Cannot read properties of undefined (reading 'inr') 未捕获的类型错误:无法读取未定义的属性(读取“公司名称”)JS 对象 - Uncaught TypeError: Cannot read properties of undefined (reading 'companyName') JS Objects Mocha js Uncaught TypeError:无法读取未定义的属性(读取“应该”) - Mocha js Uncaught TypeError: Cannot read properties of undefined (reading 'should') 错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“数据”) - ERROR: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data') 错误:未捕获的类型错误:无法读取未定义的属性(读取“地图”) - Error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') ethers.js 合同 object 出现错误:未捕获类型错误:无法读取未定义的属性(读取“地图”) - ethers.js Contract object gets error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM