reject node new from example javascript node.js express promise bluebird

javascript - new - La promesa de NodeJS Bluebird creada en un controlador pero no fue devuelta



node js (1)

Tengo el siguiente código de nodojs como una función de middleware express

Middleware.is_authenticated = function(req, res, next) { if(req.system_session == undefined || req.system_session.login_status == false) return res.status(401).send({errors: true, error: ''invalid_session'', session: req.system_session}).end(); Session.validate_session(req.system_session, req.ip, req.headers[''user-agent'']) .then(function(session) { return next(); }) .catch(function(err) { req.system_session.destroy(); return res.status(401).send({errors: true, error: err.message, session: req.system_session}).end(); }); };

Mi Session.validate_session code:

Session.validate_session = function(user, user_ip, user_agent) { return new Promise(function(resolve, reject) { if(user.user_id == null || user.user_name == null || user.user_rank == null || user.user_session == null || user_ip == null || user_agent == null) return reject(new Error(''invalid_session'')); new api_session({user_session: user.user_session}).fetch({columns: [''user_id'', ''user_name'', ''user_rank'', ''user_session'', ''user_ip'', ''user_agent'']}) .then(function(result) { if(result == null) return reject(new Error(''invalid_session'')); return result.toJSON(); }) .then(function(session) { if(session == null || session.user_id != user.user_id || session.user_name != user.user_name || session.user_rank != user.user_rank || session.user_ip != user_ip || session.user_agent != user_agent) return reject(new Error(''invalid_session'')); return resolve(session); }) .catch(function(err) { return reject(err); }); }); };

Todo está funcionando exactamente como debería, todo se ejecuta sin problemas con la base de datos, así como resolver la ''siguiente'' ruta e incluso mostrar la respuesta. Pero no importa lo que intento, sigo obteniendo ''una promesa creada en un controlador pero no fue devuelta''

Warning: a promise was created in a handler at middleware.js:12:11 but was not returned from it at new Promise (/Users/Bill/Documents/app/node_modules/bluebird/js/release/promise.js:77:14) at Object.Permission.permission_list (/Users/Bill/Documents/app/app/security/permission.js:8:10) at PermissionService.permission_list (/Users/Bill/Documents/app/app/http/services/permission_service.js:6:14) at Layer.handle [as handle_request] (/Users/Bill/Documents/app/node_modules/express/lib/router/layer.js:95:5) at next (/Users/Bill/Documents/app/node_modules/express/lib/router/route.js:131:13) at /Users/Bill/Documents/app/app/http/middleware.js:12:11 at processImmediate [as _immediateCallback] (timers.js:383:17) From previous event: at Middleware.is_authenticated (/Users/Bill/Documents/app/app/http/middleware.js:10:4) at Layer.handle [as handle_request] (/Users/Bill/Documents/app/node_modules/express/lib/router/layer.js:95:5) at next (/Users/Bill/Documents/app/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/Users/Bill/Documents/app/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/Bill/Documents/app/node_modules/express/lib/router/layer.js:95:5) at /Users/Bill/Documents/app/node_modules/express/lib/router/index.js:277:22 at Function.process_params (/Users/Bill/Documents/app/node_modules/express/lib/router/index.js:330:12) at next (/Users/Bill/Documents/app/node_modules/express/lib/router/index.js:271:10) at clientSession (/Users/Bill/Documents/app/node_modules/client-sessions/lib/client-sessions.js:630:5) at Layer.handle [as handle_request] (/Users/Bill/Documents/app/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/Users/Bill/Documents/app/node_modules/express/lib/router/index.js:312:13) at /Users/Bill/Documents/app/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/Users/Bill/Documents/app/node_modules/express/lib/router/index.js:330:12) at next (/Users/Bill/Documents/app/node_modules/express/lib/router/index.js:271:10) at SendStream.error (/Users/Bill/Documents/app/node_modules/express/node_modules/serve-static/index.js:121:7) at emitOne (events.js:77:13) at SendStream.emit (events.js:169:7) at SendStream.error (/Users/Bill/Documents/app/node_modules/express/node_modules/send/index.js:275:17) at SendStream.onStatError (/Users/Bill/Documents/app/node_modules/express/node_modules/send/index.js:392:12)

Si alguien tiene alguna solución, sería genial. Tengo muchas promesas en mi proyecto que son extremadamente similares y todas funcionan estupendamente, pero no puedo entender esto.


Esto proviene de la combinación de código basado en promesas ( Session.validate_session ) con código basado en devolución de llamada ( next ). Sería de esperar que la next() llamada no sea asincrónica (no cree ninguna promesa) o devuelva esa promesa, que no (culpa de expresar).

Hay dos formas de evitar la advertencia:

  • Deje que express haga el manejo de errores, y llame a next con un error si ocurre. En este caso, puede usar el método .asCallback :

    Middleware.is_authenticated = function(req, res, next) { if (req.system_session == undefined || req.system_session.login_status == false) var promise = Promise.reject(new Error(''invalid_session'')); else var promise = Session.validate_session(req.system_session, req.ip, req.headers[''user-agent'']); promise.asCallback(next); };

  • Como dice la explicación de advertencia , puede return null explícitamente en lugar de devolver el valor undefined que arroja next() :

    Middleware.is_authenticated = function(req, res, next) { if (req.system_session == undefined || req.system_session.login_status == false) var promise = Promise.reject(new Error(''invalid_session'')); else var promise = Session.validate_session(req.system_session, req.ip, req.headers[''user-agent'']) .catch(function(err) { req.system_session.destroy(); throw err; }); promise.then(function(session) { next(); return null; }, function(err) { res.status(401).send({errors: true, error: err.message, session: req.system_session}).end(); }); };