Within my ejs webpage, I am implementing a try catch block within a script tag.
Sometimes when rendering the page, my server includes the token and user, while other times it does not. In previous instances where variables were passed through, I would use
var x = <%- JSON.stringify(x) %>;
. However, this time, due to nothing being passed in, an error occurred.
The main issue I'm facing is that despite trying to initialize these variables, they do not seem to work as intended. Here is the code snippet:
try {
var Token = <%- JSON.stringify(Token) %>;
var User = <%- JSON.stringify(User) %>;
}
catch(err){
console.log("there is no token/user");
}
My intention is for the code to attempt to set these variables if they are passed in from the server's res.render function. If not provided, the code should continue execution without any problems.