Reputation: 890
I am trying to pass the token to the email Template which is an ejs
file i.e emailTemplate.ejs
But It's not getting rendered as the data instead it's getting rendered only as test i.e token
variable is rendered as token
text.
Here is the code:
sendgridMail.setApiKey(sendGridApi)
const email = async (req, res, next) => {
const user = await User.findOne({ email: req.body.email })
let token
if (user) {
token = user.tokens.slice(-1)[0].token
console.log('token before sending email ', token)
}
const filePath = path.join(__dirname, '/../../views/emailTemplate.ejs')
// TO PASS TOKEN TO EMAIL TEMPLATE EJS FILE
ejs.renderFile(filePath, { token: token }, (err, data) => {
if (err) {
console.log('Error in renderFile ', err)
} else {
// console.log('ejs data ', data)
const message = {
to: req.body.email,
from: '[email protected]',
subject: `Welcome to Donut ${req.body.name.firstName}`,
html: data
}
sendgridMail.send(message).then(
() => {
console.log('sending email')
next()
},
(error) => {
res.status(HttpStatus.BAD_REQUEST).send({
error: process.env.SENDGRID_API_KEY
? error.response.body
: 'Setup SENDGRID_API_KEY environment variable'
})
}
)
}
})
}
HERE IS EMAIL TEMPLATE EJS trying to render the URL
<p>http://localhost:5000/user/activate/ <=% token %></p> // TOKEN IS NOT GETTING RENDERED HERE
Whreas alraedy configured as ejs
veiw engine in server.js
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
Upvotes: 0
Views: 46
Reputation: 29
I think it's just a ejs syntax error. Passing a parameter to a template should look like this.
<%= token %>
Upvotes: 1