Day 74: Variable Not Defined EJS – SOLVED

Continued working on the final project in the web security section of the Node.js course from The Odin Project. I was building out the Membership code feature. This feature allows a registered user to enter a secret code to gain access as a “member” on the application. There was nothing too difficult about the task since I was able to reuse some code from the “Add Message” controller and apply it here. The problem came from a quirk in the ejs package. I was trying to display error messages when the user entered the wrong membership code but I kept getting an error message from my ejs template stating that the errors variable I was passing in was undefined.

So, like everyone else I went to google to see if someone else had this problem and hopefully they would provide a solution. I found a good stack overflow answer but all the answers lacked a clear explanation as to why this was happening in the first place. Personally, I still don’t fully know why this is happening but I have some thoughts which I jotted down and shared below.

I really wanted to go through and try to once again reverse engineer another npm package but I have to stay focused. As tempting as it may be to crack open the ejs package, I really do need to move on because I am starting to get diminishing returns on my time spent in this section. So for now I will just note down the issue and my preferred solution and keep it moving.

TLDR;

Okay, so here are the highlights of what I did:

  • Backend -> Continued working on the Members Only Project from the web security section. Here is the Repo where you can see the progress. Finished building out the “Become a Member” functionality. I still have the “Become an Admin”, and a few other smaller features left. Almost there!

ejs Bug Notes

All Variables Passed To Templates Must Be Declared For Each Render Regardless Of If They Are Used Or Not.

Context: express application

If you do not pass a variable that will be used in an ejs template (view) in ALL res.render('view', {...variables}) calls you will end up with the variable is not defined error which is really annoying. I struggled to solve this issue for a while. I thought that things were simple because my currentUser variable seemed to work just fine. I was completely wrong because that value (currentUser) is constantly passed to my templates by a global middleware function in the app without my notice. So even if a user is not currently logged in the value is still defined as undefined and passed to any loaded template.

ejs does not operate with the normal undefined variable type. If the variable has not been passed along to the template it will be viewed as being undefined (or rather like it doesn’t exist at all) and a server error will be thrown. However, if you pass the variable to the template even if the value you provide is undefined, things will work just fine because the variable now exists within the template’s scope.

res.render("membership", { errors: undefined });

//... Later on in the POST handler
res.render("membership", { errors: errObj.errors });

This will work but if you had not declared the errors object when the template was first loaded, it seems like the issue will remain and you will keep getting errors. I wish someone told me about this LOL. I struggled for a good hour on this issue.


Goal For Round 8 of the #100DaysofCode Challenge

This is my eighth round of the “#100daysofcode” challenge. I will be continuing my work from round five, six, and seven into round eight. I was working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal was to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in turn derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I am currently putting a pause on the algorithm work to build some backend/full stack projects. I primarily want to improve my skills with the back-end from an implementation perspective. I have improved tremendously in terminal and CLI skills but I lost focus due to how abstract the algorithm concepts got. I wanted to work on things that were more tangible until I can get to a position where I could directly benefit from improving my algorithm skills and theoretical knowledge. So that’s the focus right now. Build my backend skills and prove my full stack capabilities by building some dope projects.

Again, I still have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects. Best case scenario I actually become one of those unicorn developers that go on to start a billion dollar company… You never know LOL.