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
CodePudding user response:
I see 2 issues in the code:
getInfo
is async function and there is no callback / await added when invoking it in controller.- 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);
});
};
CodePudding user response:
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
);
};