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

669
Visualizações
parameter is declared but its value is never read... why not

To send an email, I have a controller method as such:

const send = function (subject, template, to, options) {
// VSC says about "subject": is declared but its value is never read. 
// VSC does not say this for the other three parameters.
    return new Promise(function (resolve, reject) {
        fs.readFile(template, "utf8", function (err, templateContent) {
            if (err) {
                return resolve(false);
            }

            var subject = subject;
            console.log(subject);
            // this is where I do read "subject" but it returns 'undefined'
            // (even though I am passing the function a value for the parameter)

            ... etc

What am I doing wrong? In my mind, I have declared a parameter subject and I use later on in the controller method.

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

0

var subject = subject;

var subject creates a new variable, named subject in the scope of the callback function passed to readFile.

This shadows the subject variable created as a parameter name for the function assigned to send.

var subject = subject; therefore copies the value of the local subject (which is undefined at the time) to subject (which does nothing).

Give your variables different names even if they do similar things.

I recommend the use of a linter which can enforce a rule like no-shadow (you seem to be using one already — VS Code won't generate the error message, only report it from a tool which does — so make sure you turn that rule on for that tool).

about 4 years ago · Juan Pablo Isaza Relatório

0

var inside the function will be hoisted to the top so at runtime it is:

fs.readFile(template, "utf8", function (err, templateContent) {
  var subject;
  // ...
  subject = subject; //undefined
});

Just use another name for your nested subject like _subject and avoid shadowing variable names in general.

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