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

211
Visualizações
How to pass in Apps Metadata to a Class Method?

In refactoring my Bug Report code I moved a function into a new Class 'Logger', and call the static method as seen below:

 $("#bugForm").submit((e) => {
        e.preventDefault()
        const input = document.getElementById('nameInput');
        bugInfo = {
          "name": `[${ticket.id}] Bug report`,
          "story_type" : "Bug",
          "description": `+ ${urlHelper.zendeskTicketUrl}` + " \n" + `+ ${input.value}`,
        }
        Logger.logInfo(bugInfo).then(collapse.collapse('toggle'))
      })
    });

however when I run the static method I receive the following error:

  Uncaught (in promise) ReferenceError: metadata is not defined

Logger.js

 class Logger {

  constructor(settings) {
    this.settings = settings;
  }
    static async logInfo(data = {}) {
      console.log('Hello!')
      const url = 'exampleUrl'
      const response = fetch(url, {
        method: 'POST',
        headers: {
          "Token": `${metadata.settings.token}`,
          "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
      });
      return response.json();
    }
}

In an attempt to fix this, I placed the following line in my code:

const logger = new Logger(metadata.settings);

And received the following error:

Uncaught (in promise) ReferenceError: Cannot access 'Logger' before initialization

I originally only made the class to use its static method, does the need for metadata prevent me from doing this? Am I not using this correctly?

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

0

So the problem the way you're passing the metadata.setting

I have changed way you use setting. Here's a working snippet

$("#bugForm").submit((e) => {
  e.preventDefault()
  const input = document.getElementById('nameInput');
  // logic here
  const bugInfo = {
    info: "Hello"
  }
  // changed here as I removed static
  logger.logInfo(bugInfo).then(console.log('print'))
});

class Logger {

  constructor(settings) {
    // getting the settings here and assigning it to the constructor variable
    this.settings = settings;
    console.log('hello', this.settings)
  }

  // removed static
  async logInfo(data = {}) {
    console.log('Hello!')
    const url = 'exampleUrl'
    console.log(data);
    console.log(this.settings)
    const response = fetch(url, {
      method: 'POST',
      headers: {
        // using it here while calling the method
        "Token": `${this.settings.token}`,
        "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
    });
    return response.json();
  }
}


const metadata = {
  settings: {
    token: 'hello'
  }
}
const logger = new Logger(metadata.settings);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="bugForm">
  <button type="submit">
Submit
</button>
</form>

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