Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
Nodejs - Call a service from a typescript controller

In my controller I have this method that renders a marko file sending an object to the view with data (dataView)

export const renderRecoveryData = (
  req: Request | any,
  reply: ResponseToolkit,
  callback: any
): void => {
  const dataView: { [key: string]: any } = {
    id: 1,
    info: getInfo(req, reply, callback),
  };
  templateEngine.renderFromTemplate(
    "./views/private/info/info.marko",
    dataView,
    req,
    reply,
  );
};

In the "info" property I want to load an object that obtains the data from the call to two services, using this method:

const getInfo = (req: Request | any, reply: any, callback: any): void => {
  const headers = helpers.setHeaders(req);
  const idNumber = req.yar.get('idNumber');
  async.parallel({
    personalData: (callback: any) => infoService.personalInfo(idNumber, headers, callback),
    addionalInfo: (callback: any) => infoService.addicionalInfo(headers, callback),
  }, (err: any, responses: any) => {
    if (err) {
      return callback(err);
    }
    console.log("response", responses);
    return responses;
  });
};

The console.log correctly shows me the object that I want to save in the "info" property of "dataView", but when I check what the view gets, the "info" property doesn't show up, it just shows me the "id" property

I call "renderRecoveryData" from a routes file:

  server.route({
    method: "GET",
    path: "/{locale}/private/info/dashboard/step1",
    handler: renderRecoveryData,
  });

What can be the problem? Thanks

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I see 2 issues in the code:

  1. getInfo is async function and there is no callback / await added when invoking it in controller.
  2. Callback is not invoked on success in getInfo function.

Changing code to something like below should work:


export const renderRecoveryData = (
  req: Request | any,
  reply: ResponseToolkit,
  callback: any
): void => {

  getInfo(req, reply, (err, info) => {
    if(err) {
      // Handle error
    }

    const dataView: { [key: string]: any } = {
      id: 1,
      info,
    };
   
    templateEngine.renderFromTemplate(
      "./views/private/info/info.marko",
      dataView,
      req,
      reply,
    );
  });
  
};

const getInfo = (req: Request | any, reply: any, callback: any): void => {
  const headers = helpers.setHeaders(req);
  const idNumber = req.yar.get('idNumber');
  async.parallel({
    personalData: (callback: any) => infoService.personalInfo(idNumber, headers, callback),
    addionalInfo: (callback: any) => infoService.addicionalInfo(headers, callback),
  }, (err: any, responses: any) => {
    if (err) {
      return callback(err);
    }
    console.log("response", responses);
    return callback(err, responses);
  });
};

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think the issue is with the way you are returning response, I think you should pass the response to the callback

const getInfo = (req: Request | any, reply: any, callback: any): void => {
  const headers = helpers.setHeaders(req);
  const idNumber = req.yar.get("idNumber");
  async.parallel(
    {
      personalData: (callback: any) =>
        infoService.personalInfo(idNumber, headers, callback),
      addionalInfo: (callback: any) =>
        infoService.addicionalInfo(headers, callback),
    },
    callback // this is better
  );
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda