Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
¿Cómo pasar metadatos de aplicaciones a un método de clase?

Al refactorizar mi código de informe de error, moví una función a una nueva clase 'Registrador' y llamé al método estático como se ve a continuación:

 $("#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')) }) });

sin embargo, cuando ejecuto el método estático, recibo el siguiente error:

 Uncaught (in promise) ReferenceError: metadata is not defined

Registrador.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(); } }

En un intento de arreglar esto, coloqué la siguiente línea en mi código:

 const logger = new Logger(metadata.settings);

Y recibió el siguiente error:

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

Originalmente solo hice que la clase usara su método estático, ¿la necesidad de metadatos me impide hacer esto? ¿No estoy usando esto correctamente?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Entonces, el problema es la forma en que estás pasando los metadatos.

He cambiado la forma en que usas la configuración. Aquí hay un fragmento de trabajo

 $("#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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!