Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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.

<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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

replace() is a function that acts on string values.

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.

   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:

You can use a ? optional chaining operator to only execute replace function if the value is not 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);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda