Tengo un problema con la ejecución de una función de devolución de llamada. Acabo de crear una función de ayuda para que la utilice el enrutador y para fines internos. La función auxiliar funciona perfectamente para la parte del enrutador (lo intenté con el cartero), pero cuando intento ejecutar la función auxiliar sola, surgen problemas.
const MyModel= require('../model/MyModel'); const ERR_REASON = { SERVER: 'SERVER_FAULT', DB: 'DB_FAULT', }; const getCommunicationDiagnosticsHelper =function (start, end, cbf) { /* If I execute my callback function here, it works */ MyModel.aggregate([ //aggregator operators are passed here, //I'm not providing the pipeline operators list but they work fine! ]) .allowDiskUse(true) .exec(function (err, results) { // START-This part of code is never executed in my function if (err) { cbf(true,ERR_REASON.DB,null) } try { cbf(false,null,results) } catch (error) { cbf(true,ERR_REASON.SERVER,null) } // END-This part of code is never executed in my function }); };Esto funciona bien,
function cb(err,info,data){ console.log({err,info,data}) //supposed everything went well //this refers to `res` this.status(200).json(data) } const getCommunicationDiagnostics = function (req, res, next) { // start and end variables are to be used in aggregation. // parseDate is a simple utility function, const { start, end } = parseDate(req.params.interval); getCommunicationDiagnosticsHelper(start, end, cb.bind(res)) }; //router.js const router=require('express').Router(); router.get('/com_diagnostics',getCommunicationDiagnostics)Pero esto no. Espero que se ejecute la función de devolución de llamada (como argumento), pero no se está ejecutando.
getCommunicationDiagnosticsHelper('2021-01-19T19:59:59.999Z','2021-01-20T19:59:59.999Z',(err, info, data) => { //This part of code is never reached! console.log({err,info,data}); })¿Por qué la función auxiliar completa la ejecución a pesar de que la función de devolución de llamada no se ejecuta?