Categories
Uncategorized

How to fix ng-if error in AngularJS

The ng-if directive has it’s own scope, so if you have issues with why a certain component is added or removed from the DOM even though your condition is true. It is probably because you aren’t referring to the right property. So this is how that can be fixed easily by referring to the parent scope.

Categories
Uncategorized

User Registration Form using PHP 7.1.5

github

The first step would be to create a new MySQL database and then add a user with all privileges except GRANT. Now replace the values in the file db_connection.php with those.

Now once you are done with this run the file createTables.php from the browser. You should see a success message. If not make sure you have entered the right credentials and that MySQL server is running.

Now, if you don’t have a registration form UI (HTML5+js) then you can use postman from the chrome web store or any other testing tool.

You can use the following JSON to post to userRegistration.php

This should give you a success message when executed once and you should see the row added to your database in your PhpMyAdmin. If executed more than once, then you will get a message “User already exists”.

Note: Make sure that you have PHP 7.1.5 installed. This program works on HHVM as well.

Categories
Uncategorized

How to Automate your Releases using only git & crontab?

You can easily automate your code deployment using crontab and git.
Lets assume you push your code to your remote PROD branch and a cronjob on prod keeps running or runs at a certain time checks if your branch is behind the remote or not and if it is, then it pulls code and restarts your application.

To start off make sure you have both git and a git repository. If you do then you can skip to Automation, if not then start by installing git and initializing a git repository on your local. For Ubuntu, here is how,

Note: replace ‘https://github.com/user/repo.git’ with your HTTPS git repo URL.
If you see an error that remote already exists then you can use the following to remove it and then add your new repository under the same name or you can add a new name

                                                          or

Automation

Replace ‘/your-path-to-git/project’ with the path to your git project.
replace ‘branch-name’ with the branch you want to automate.
Save this in a file called automate.sh.

Replace both the occurences of ‘branch-name’ with your required branch name and then save the code in a file named ‘pull_code.sh’ in the same directory as the ‘automate.sh’.

If you run the file using the following command then it should check for changes in the remote branch and pull them. Make sure to change ‘branch-name’ to whatever branch name you are using.

Make the two files executable by the user

Cronjob

The next step would be to create a cronjob for this using crontab.
Use the following command for that,

Then hit ‘i’ start editing using vi (Visual Editor – The default editor that comes with the UNIX operating system).

You can add your email in the MAILTO parameter.  But this works only if  you don’t have logging set, here we have set all the output and standard error output to null, so it won’t work. For the email functionality to work, you need to have the mail server setup for this.

To log all the output to a file, replace the job with the following.

After adding the above code to your crontab, use the following to write changes and quit the vi.
‘ESC’ + ‘wq!’

This job is set to run every minute. It should have already output some data into your logs. Just to be sure we can restart the cron service once after the changes are saved.

Categories
Uncategorized

Handling Date objects in Javascript

Handling date objects in javascript can be very tricky at times. Here are few tricky scenarios.

Comparision with ‘===’

When you compare two date objects that contain the exact same time stamp using ‘===’, then this will not be true as two objects can only be equal if they refer to the same object.

Output

Demo

The solution to this, is to convert this to some other format like date.getTime() or date.getISOString(), etc. Here is the example implementation of that.

Output

Demo

Copying a Date Object

When you try to copy date objects like the following example, then both the variables refer to the same date objects and whatever changes you make on either of them reflects on the other, which is not the required outcome

Output

Demo

To solve this we need to create a new Date object while assigning the date to a new variable.

Output

Demo

Categories
Uncategorized

Sort javascript objects based on multiple properties

When you want to sort javascript objects based on 2 properties,  you can use the following code. In this example here we have an array of people (objects) here. I want to sort them by first name and then by their last names. So, what I do in my compare function to get the required output is to sort them by their last names, only when their first names are equal.

Output

Demo

If you would like to use integers then, this example with ages would help. Here the people are sorted by first names and then by their ages.

Output

Demo

If you would like to sort objects in descending order (strings or integers), then just return the negative of the result.

Output

Demo