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

198
Views
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 answers
Answer question

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 Report

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 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!