Tag: debugging

Type error, Undefined. Most common mistakes in Javascript and how to resolve them.

Javascript is almost everywhere on web. But sometimes we do make mistakes in writing the code. Here we will see how to solve them.

How to debug or see the errors

Press F12 in browser it will open developer tool. There click on console tab. Here you can see warnings and errors or the log statements that are part of your Javascript code. You can choose on what to see in errrors/warnings/log statements etc.

Undefined variable

If you are getting Type error undefined most often it is the variable its referring to. It is either not storing a value or is not declared as var.

User input validation

Make sure to remove extra white spaces using Javascript’s trim() function.

If your if condition variable is not matching as expected

Then make sure your variable’s type is correct. That is sometimes you might be matching a string but you might be storing it as a integer/float. Just print the type of the variable to see what is happening.

How to debug your code in PHP and most common mistakes in PHP.

Here we will see some tips and most common problems that programmers generally encounter while writing PHP code.

Common errors or mistakes and debugging tips

  • First and foremost thing is check your error.log file. For Ubuntu it is at /var/log/apache2/error.log or most Linux distribution this file is at /var/log/htttp/error.log.
  • Open a terminal and type tail -f /var/log/apache2/error.log. This command will open the file and in be in waiting mode and it will update as any changes are made it writes to the standard output. After running this command just type enter button for 2 or 3 times so that you can cleary distinguish between previous errors and new errors. Of course you can distinguish with the timestamp from the log too. Now just execute the PHP code and see the errors in the terminal. You may have a look at this image.
  • Now even after seeing the log you may have found some errors and fixed them. But still your code might not be doing what you want it to do. So now look for logical errors that you might have done. To find this errors write print/echo statement at crucial points and see if the variable is storing the value is as expected.
  • Another most common error the permission denied error when dealing with files. To solve this you can give your files that are throwing this error the www-root:www-root permission if you are on Ubuntu or apache:apache in other Linux distributions. This can be done using chown command. For example chown www-data:www-data file.txt.
  • If you are using Mysql to connect to database and a query is not executing using the variables it is receiving print that query just before it executes and then try the same query in your mysql command line interface and see if if fetches the desired result or throws any error.

These are the common tips and errors that I encounter and I had given tips from my experience. I will update this list as I do more. Thank you.