Home > Blockchain >  The code flow stops after receiving a promise
The code flow stops after receiving a promise

Time:08-25

I have a simple function that validates a token on a user record.

@Get("confirm/:token")
    verifyEmail(@Param("token") token: string, @Res() res: Request) {
        console.log("confirm called")
        console.log(token);
        const user = this.userRepository.findOne({where: {token: token}});
        console.log("user promise")
        return res.res?.redirect("/home/welcome");
    }

when i run the code i can see in the console "user promise" but no progress at all, what could posibily be the issue?

CodePudding user response:

You inject the Response object but tell Typescript that you use the Request object, so Typescript can't warn you that res.res.redirect isn't actually what's right there. You should be using the Response type from express and call res.redirect instead of res.res?.redirect.

The reason you get no error is the optional operator, if the object you access is null or undefined, it doesn't go any further in the execution.

Or, as you found out, you can use the @Redirect() decorator, just make sure to remove the @Res() injection as it's unnecessary

CodePudding user response:

i was able to resolve my issue by using the Redirect() decorator as follows

@Get("confirm/:token")
    @Redirect('http://localhost:3000/home/welcome', 301)
    verifyEmail(@Param("token") token: string, @Res() res: Request) {
        console.log("confirm called")
        console.log(token);
        const user = this.userRepository.findOne({where: {token: token}});
        console.log("user promise")
        user.then((u) => console.log(u));
        return res.res?.redirect("/home/welcome");
    }
  • Related