0

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

3
  • This is happening because getInfo is async function and you will have to either use await when calling it or render template in callback of getInfo. Other problem is you are not invoking callback in case of success (please replacereturn responses with return callback(null, responses)). Commented Dec 30, 2021 at 8:32
  • I am testing your recommendation, thank you Commented Dec 30, 2021 at 8:56
  • I added answer with detailed code changes. I hope it will help. Commented Dec 30, 2021 at 9:04

2 Answers 2

1

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);
  });
};

Sign up to request clarification or add additional context in comments.

2 Comments

The solution works perfect, thank you very much (I just had to add "=>" in the "getInfo" function)
Yes missed it, edited answer. Glad it helped.
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
  );
};

3 Comments

Yes, the problem I also think is how I return it, I tried the callback, but nothing, the info property does not appear with the object and it does not give any error, thanks
from where renderRecoveryData function is called.
It is called from a typescript routes file, I update the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.