Routing-controllers: Async response

Created on 2 Feb 2018  路  7Comments  路  Source: typestack/routing-controllers

Hi everyone,

I'm having a issue when I try to do a response async by example:

    @Get('/')
    create(@Req res, @Res res) {
        event.on('response-from-cqrs', () => {
           res.json(<response>);
        });
    }

seems that this isn't possible, is there some way the able to do that without do a return?

Thank you so much !

question

Most helpful comment

You need to wrap it with the promise:

@Get('/')
create(@Req() req, @Res() res) {
    return new Promise(resolve => {
        event.on('response-from-cqrs', () => {
            resolve(<response>);
        });
    });
}

Or:

If you want to handle the response by yourself, just make sure you return the response object itself from the action.

@Get('/')
create(@Req() req, @Res() res) {
    event.on('response-from-cqrs', () => {
        res.json(<response>);
    });
    return res;
}

All 7 comments

You need to wrap it with the promise:

@Get('/')
create(@Req() req, @Res() res) {
    return new Promise(resolve => {
        event.on('response-from-cqrs', () => {
            resolve(<response>);
        });
    });
}

Or:

If you want to handle the response by yourself, just make sure you return the response object itself from the action.

@Get('/')
create(@Req() req, @Res() res) {
    event.on('response-from-cqrs', () => {
        res.json(<response>);
    });
    return res;
}

@19majkel94 The first piece of code worked correctly, thank you very much!

@19majkel94 @NoNameProvided I would expect the below block to work, unless I have totally misunderstood something:

@Get('/test/callback')
public async onLoginWithOauthProvider(@Req() request: express.Request, @Res() response: express.Response): Promise<any> {
    return new Promise((resolve, reject) => {
        // Placeholder for some async work, e.g. fetching user data from OAuth service
        setTimeout(() => resolve({ foo: 'bar' }), 1000);
    });
}

However, it returns a 404 and complains that Error: Can't set headers after they are sent.. I am using routing-controllers version 0.7.7. Thanks for your help.

Because when you use @Res() decorator, you declare that you take care of handling response to client by yourself. You should use res.send along with return res to prevent automatic headers sent.

@19majkel94 thanks for your response. If I understand you correctly, it should work like this:

@Get('/test/callback')
public onLoginWithOauthProvider(@Req() request: express.Request, @Res() response: express.Response): express.Response {
        setTimeout(() => response.send({ foo: 'bar' }), 2000);
        return response;
}

Unfortunately, I am experiencing the same behaviour.

@19majkel94 would be awesome if you could help me out with this. Thanks

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

circy picture circy  路  3Comments

iangregsondev picture iangregsondev  路  3Comments

impzero picture impzero  路  4Comments

mlarsson picture mlarsson  路  5Comments

azaslonov picture azaslonov  路  5Comments