简体   繁体   English

未捕获的类型错误:无法读取未定义的属性(读取“替换”)。 我在我的网站上遇到了这个错误

[英]Uncaught TypeError: Cannot read properties of undefined (reading 'replace'). I am facing this error in my site

I am using this code on the WordPress website and errors continuously coming to my site.我在 WordPress 网站上使用此代码,并且错误不断出现在我的网站上。

<script>
            function setNiceResult(valor, id){
                var div = $("#"+id);
                valor = valor.replace(/\{\{/g, "<strong class=\"fuerte\">").replace("}}", "</strong>");
                valor = valor.replace(/\}\}/g, "</strong>");
                valor = valor.replace(/\{/g, "<strong class=\"medio\">").replace("}", "</strong>");
                valor = valor.replace(/\}/g, "</strong>");
                div.html(valor);
                window.redrawMasonry(); // NO BORRAR!
            }
        </script>

This picture shows the error此图显示错误

The reason why you are receiving this error is because the variable valor does not have a value assigned to it and thus is undefined .您收到此错误的原因是因为变量 valor 没有分配给它的值,因此是undefined

replace() is a function that acts on string values. replace()是一个作用于string值的函数。

PROBLEM:问题:

 var sentence; // value is undefined sentence = sentence.replace("word", "new word"); // trying to execute replace function in undefined type console.log("sentence: ", sentence);

Solution 1: To prevent the error from occuring, you can assign an empty string value.解决方案1:为防止发生错误,您可以分配一个空字符串值。

 var sentence = ''; // value is undefined sentence = sentence.replace("word", "new word"); // trying to execute replace function in undefined type console.log("sentence: ", sentence);

Solution 2:解决方案2:

You can use a ?你可以使用一个? optional chaining operator to only execute replace function if the value is not undefined :可选链接运算符仅在值undefined时才执行替换功能:

 var sentence; sentence = sentence?.replace("word", "new word"); console.log("sentence: ", sentence); var anotherSentence = "Some word value"; anotherSentence = anotherSentence?.replace("word", "new word"); console.log("anotherSentence: ", anotherSentence);

暂无
暂无

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

相关问题 我正面临“TypeError:无法读取未定义的属性(读取&#39;push&#39;)”的错误 - I am facing an error of "TypeError: Cannot read properties of undefined (reading 'push')" 未捕获的类型错误:无法读取未定义的属性(读取“替换”) - Uncaught TypeError: Cannot read properties of undefined (reading 'replace') 在反应中遇到此错误“TypeError:无法读取未定义的属性(读取'find')” - facing this error in react "TypeError: Cannot read properties of undefined (reading 'find')" 得到一个名为 Uncaught TypeError: Cannot read properties of undefined (reading &#39;className&#39;) className.replace property - Get an error called Uncaught TypeError: Cannot read properties of undefined (reading 'className') className.replace property 错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“数据”) - ERROR: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data') 错误:未捕获的类型错误:无法读取未定义的属性(读取“地图”) - Error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM