Sending Emails in Nestjs

Viewed 1296

I am trying to send email in NestJs but seems to stuck at a weird error.

TypeError: Cannot destructure property 'templateName' of 'precompile(...)' as it is undefined.  
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:363:5)
    at ServerResponse.setHeader (node:_http_outgoing:573:11)

I am not sure what's going on the only solution I was able to find was to put ''./'' in front of the template names but I am not sure why would that work also it isn't even working in my case.

This is how i am trying to send it

this.mailService.sendEmail(
            emailAddress,
            [], // cc
            [], // bcc
            "Pretty Subject Line", // subject
            "./verify-email", // template
            { // context
                name: "tetsName",
                verifyLink
            }
        );

templates folder is in root directory

1 Answers

Cannot set header error mostly occurs when you are sending a response after once you have sent a response. Take a look at the following code

//This does not work
res.send("This is a Response");
res.send("This is a second response");

If you are doing something like this, then it won't work. Try a logic that does all the tasks you want in one go or use may

res.write("This is a response"):
res.end();
Related