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

139
Views
devolver la instancia de un cliente pom en una función y acceder a ella desde otro archivo perder contexto

Tengo una clase mecanografiada de la siguiente manera:

 const client = require('prom-client'); export default class Monitoring { // public client: any; private _logger: LoggingService; public constructor(logger: LoggingService) { // this._pomClient = client; this._logger = logger; } public async executePromClient() { this._logger.debug({message: `Returning prom client isnatnce`}) const histogram = new client.Histogram({ name: 'http_request_duration_seconds1', help: 'Duration of HTTP requests in seconds histogram', labelNames: ['method', 'handler', 'code'], buckets: [0.1, 5, 15, 50, 100, 500], }); return histogram; } public async getPromClient() { this._logger.debug({message: `Returning prom client isnatnce`}) return client; }

sin embargo, la función de ejecución funciona bien y no es un problema cuando llamo a getPromClient() en mi servidor de la siguiente manera:

 const monitoring = new Monitoring( loggingService, ); // const client = require('prom-client'); const client: any = monitoring.getPromClient(); // enable prom-client to expose default application metrics const collectDefaultMetrics = client.collectDefaultMetrics;

yo obtengo

 TypeError: collectDefaultMetrics is not a function at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19)

Estoy tan confundido que solo estoy devolviendo el cliente e intentando usarlo en sercer.ts ¿por qué no funciona?

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

0

señorita espera?

 const client: any = await monitoring.getPromClient();
about 4 years ago · Juan Pablo Isaza Report

0

Declaró getPromClient() como async . Eso significa que devolvería una Promise si no la await . Como no esperó nada dentro de await getPromClient() , simplemente elimine el async para resolver el problema.

 public getPromClient() { this._logger.debug({message: `Returning prom client isnatnce`}) return client; }

Probablemente no sepa lo que significa async y simplemente lo declare en todas partes que no debería. Puedes aprender más al respecto aquí

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!