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

183
Visualizações
Lodash template validating nested objects?

Im using the template function for lodash and I want to get lodash to throw an error if the nested object property does not exist but it seems that the function only checks the properties on the initial object that you pass in.

try {
  const config = `Hey <%- customer.firstName %> <%- customer.lastName %>, How are you?`;

  const res = template(config)({
    customer: {
      firstName: "Bill",
    },
    product: {
      name: "brush",
      price: "$9.99"
    }
  });
  
} catch(e) {
console.log(e)
}

The code above returns Hey Bill, How are you?

It does not throw an error when I try to use the last name variable even though it does not exist on customer. Is there anyway to make lodash check nested objects when using the template function?

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

0

It shouldn't throw an error, because it is compiling the variable to _.escape(undefined) which returns an empty string.

It should throw an error if you try customer.lastName.anotherProp.

You can explicitly throw an error, if you want, with evaluate delimiters:

try {
    const config = `Hey <%- customer.firstName %> <% if (!customer.lastName) { throw 'Object property is not defined!' } else { %><%- customer.lastName %><% } %>, How are you?`;
    const res = lodash.template(config)({
        customer: {
            firstName: "Bill",
        },
        product: {
            name: "brush",
            price: "$9.99"
        }
    });

} catch(e) {
    console.log(e)
}
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