After filters in controller not invoked when returning json with respond #79

Closed
wct opened this Issue Mar 2, 2013 · 4 comments

Comments

Projects
None yet
5 participants

wct commented Mar 2, 2013

When returning json with respond, after filters are not invoked

var self = this;
this.respond({
'json': function() { self.res.json({ hello: 'world' }); }
})

This could be maybe fixed by adding this._devoke(); in respond in controller.js?

Controller.prototype.respond =
Controller.prototype.respondWith = function(obj) {
...
} else if (typeof op == 'function') {
op();
// added line below
this._devoke(); // or this.done();
}
...
}

Not sure if this would break somehing.

giobero commented Sep 15, 2013

I've this code after actions are defined:
controller.after('*', function (next) {
console.log('inside after');
next();
});

But after handler is never fired. My action successfully finishes it's job. Does after is fired only if error happens in action? or why isn't my after handler is invoked?

punund commented Oct 1, 2013

Confirmed. After filters, '*' or specific, are not invoked when returning JSON.

Owner

jaredhanson commented Nov 28, 2013

In 0.4.x, after filters will be applied when the response is finished. This means they will work as expected even when calling the underlying Express functions directly on the response (ie res.send() and res.json()).

See also #95

Will this also be applicable to after error filters?

I'm checking for something passed in the http post data in the before filter, and if there is, passing a "new Error()" in the next() function call...

Then in the after error filter which is triggered, I'm trying to return a JSON error message back, but having no luck... here is the before and after code:

MyController.before('*', function(next) {
var self = this;
if(self.req.body.name == 'myname') {
next();
} else {
next(new Error('sorry, you cannot proceed'));
}
});

MyController.after('*', function(err, req, res, next) {
var self = this;
self.res.json({ error: err.message });
});

Any help or guidance would be appreciated. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment