Friday, October 21, 2016

Write data in existing excel using node js

Today i finally solve a big problem in my current node js project. Actually i have a requirement to write data in existing excel document. I already have an excel so i just need to open it and write some data and then save it. But unfortunately i unable to find a way to do this from a long time. After googling a lot i found a node module "xlsx-populate". Its fulfill my requirement. You can check the more detail about it from git hub link

Below is my server code of nodeJS  :'/writeEmployeeDataExcel',function(req,res){
  var data = req.body;
  var filename = "alkalmazottak.xlsx"; /**This is my new file name**/
  var newfilelocation = "../client/app/downloads/"+filename; /**This is new location of file**/
  var readexistingfile = "../client/app/downloads/employeeTemp.xlsx";/**This is existing doc that i want to read**/
  var workbook = Workbook.fromFile(readexistingfile, function (err, workbook){
   if (err) return res.status(500).send(err);
   var sheet = workbook.getSheet(0);
   var i=0;
   for (var rowNum = 2; rowNum < parseInt(data.length)+2; rowNum++) {
    var row = sheet.getRow(rowNum);
    var title = (data[i]['title']!='') ? data[i]['title'] : ''; 
    var lastname = (data[i]['last_name']!='') ? data[i]['last_name'] : '';
    var firstname = (data[i]['first_name']!='') ? data[i]['first_name'] : '';
    var email = (data[i]['email']!='') ? data[i]['email'] : '';
    for (var colNum = 2; colNum < 6; colNum++) {
     var cell = row.getCell(colNum);
     (colNum==2) ? cell.setValue(title) : '';
     (colNum==3) ? cell.setValue(lastname) : '';
     (colNum==4) ? cell.setValue(firstname) : '';
     (colNum==5) ? cell.setValue(email) : '';
   fs.exists("../client/app/downloads/"+filename, function(exists) {
    if (exists) {

Preview :

Chears :)
Happy coding

Thursday, October 20, 2016

Password Match form validation in AngularJS

I am stuck in a form validation to change password where i need to take a specific length of password (like combination of special characters, alphabets and numbers) from user and confirm it by re-enter password. Actually i am using the ng-pattern property of angular js to match the password of both input boxes but the problem is ng-pattern is only match if your are passing simple string. It don't work to match the string with the special charecters.
See example-

Not Working :

<input type="password" name="password" class="span4" ng-model="user.password" ng-pattern="/^(?=(?:[^A-Z]*[A-Z]){2})(?=[^!@#$&*\%\^\)\(]*[!@#$&*\%\^\)\(])(?=(?:[^0-9]*[0-9]){2}).{8,}$/">
<input type="password" name="password2" class="span4" ng-model="user.password2" ng-pattern="user.password">

Working :

<input type="password" id="password" name="password" ng-model="user.password" ng-pattern="/^(?=(?:[^A-Z]*[A-Z]){2})(?=[^!@#$&*\%\^\)\(]*[!@#$&*\%\^\)\(])(?=(?:[^0-9]*[0-9]){2}).{8,}$/">
<input type="password" name="password2" class="span4" ng-model="user.password2" pw-check="password">

So to match the password of both text boxes input i created a simple directive (pwCheck) below is my compleate code :

Below is my directive :

.directive('pwCheck', [function () {
 return {
   require: 'ngModel',
   link: function (scope, elem, attrs, ctrl) {
  var firstPassword = '#' + attrs.pwCheck;
  elem.add(firstPassword).on('keyup', function () {
    scope.$apply(function () {
   var v = elem.val()===$(firstPassword).val();
   ctrl.$setValidity('pwmatch', v);

Below is my html code :

<form name="registerForm" id="registerForm" ng-submit="editAccountPassword(user, registerForm)" class="form-horizontal" novalidate>
 <div class="modal-body">
  <div class="col-md-12">
  <span class="alert alert-danger ng-cloak" ng-show="registerForm.password2.$error.pwmatch && !registerForm.password.$error.pattern">Passwords should match !!</span>
  <div class="col-md-12">
  <span class="alert alert-danger ng-cloak" ng-show="registerForm.password.$error.pattern">Password should be minimum 8 character long and it should contain 2 upper case letters, 1 special character, 2 digits & 1 lower case letter</span>
  <div class="widget-content">  
   <div class="control-group">           
    <label for="password" class="col-md-4 col-sm-4 col-xs-12 field-name text-padding">New Password</label>
    <div class="col-md-8 col-sm-8 col-xs-12">
     <input type="password" id="password" name="password" class="span4" ng-model="user.password" required="" ng-pattern="/^(?=(?:[^A-Z]*[A-Z]){2})(?=[^!@#$&*\)\(]*[!@#$&*\)\(])(?=(?:[^0-9]*[0-9]){2}).{8,}$/">
   <div class="control-group" ng-class="{'has-error': registerForm.password2.$invalid && registerForm.password2.$dirty}">           
    <label for="password" class="col-md-4 col-sm-4 col-xs-12 field-name text-padding">{{'Retype New Password' | translate}}</label>
    <div class="col-md-8 col-sm-8 col-xs-12">
     <input type="password" name="password2" class="span4" ng-model="user.password2"  required="" pw-check="password">

Preview :

Happy Coding :)

Friday, September 23, 2016

Export and Import database in mongo db

Exporting from MongoDB :

To export the database, simply tell mongodump which database (or collection) you want to export, and where to export it to. Mine was the fairquid database, so my command looks like this:

mongodump -d fairquid -o fairquidbackup

This dumps the fairquid database into the fairquidbackup directory. Take a look at what we have in that directory now:

├── users.metadata.json
└── users.bson
└── products.metadata.json
└── products.bson
└── companies.metadata.json
└── companies.bson
└── system.indexes.bson

0 directories, 2 files

Some of the collection in my fairquid database are users, products, companies etc, however you'll see a .bson and .json file for each collection in your database, plus the system indexes collection. It is up to you whether you want to take individual collections, or a whole database, but bear in mind that your choice will dictate whether you get information about indexes etc when you import the data elsewhere.

Importing to MongoDB :

To import, simply use the mongorestore command, which accepts either a single .bson file representing a collection, or a directory containing multiple files. Here's my example:

mongorestore -d fairquid /path/to/fairquid

You can specify any database name and path to files you like, so for taking backups or restoring additional copies of a database, this can be really handy. The mongo commands are well-documented and I found them easy to work with.
Hopefully this helps you work with them too!

Please don't forgot to leave a comment.

Chears :)
Happy Coding

Sunday, August 21, 2016

How to detect “no internet connection” in AngularJS

Today i need to show some message when internet connection lost. So I define this interceptor in httpProvider in my app.js file, so its return an error when a call does not happen successfully due to internet connection lost. This Interceptor return status= "-1" when ajax call is failed. So i set a variable online= true here and in my html i display a message if online=true. See below my html code as well

angular.module('YourAppName', []).config([
      function($q,$rootScope) {
        return {
            responseError: function(res){
                //Angular returns "success" by default, but we will call "error" if data were not obtained.
                if( == null && res.status === -1 && res.statusText === ""){
      $ = true;
                    return $q.reject(res) //callback error()
                return res //return default success()

Now i need to show some message when $ = true;
Below i check that online variable is true then show a message "Your have lost your internet connection. Check your internet connection and reload your page again." other wise not.

<!--Below message is showing when internet connection is lost. --> 
<div class="alert alert-error" ng-if="online"><i class="btn-icon-only icon-warning-sign"></i>
 Your have lost your internet connection. Check your internet connection and reload your page again.<button ng-click="reloadRoute()"><i class="icon-refresh"></i> Reload</button>

If you see my above html there i also add a reload button to reload the page to do this i create a function and i call this on click of reload button Below is my function :

// This function Is use to reload the active page.
$rootScope.reloadRoute = function() {

Now in the last step you need to hide the above message automatically when internet connection is coming again to do this i just put $ = false; when view content loaded like below :

// To set the view online message false.
$rootScope.$on('$viewContentLoaded', function() {
   $ = false;

Preview :

If you like this post or you have any problem during implementation don't to leave a comment below i will definitely reply you back as soon as possible.

Chears :)

Happy coding

Friday, June 24, 2016

How to remove html in angular js

Suppose you  have a textarea editor and you entered some text in it and making some formatting like change font or adding style etc . So now you want that when you are showing it you don't want to shown html you adding in it. See screenshot below.

Remove html in angular js

To do this below is a simple filter you can use : You just need to be add this filter in your app.js and change the "yourAppName" with your own.

yourAppName.filter('htmlToPlaintext', function() {
    return function(text) {
      return  text ? String(text).replace(/<[^>]+>/gm, '') : '';

Method of calling this filter :

<label style="color: #636363;">{{company.notes | htmlToPlaintext}}</label>

If you like this post don't forgot to leave a comment.

Happy Coding :)

Thursday, June 23, 2016

Difference between php-4 and php-5

This is a very common interview question. If you are going for the interview then you have to know the difference between these. Below is the basic difference of PHP-4 and PHP-5 -

1- PHP-4 is a procedural language where as PHP-5 is a object oriented language.

2- PHP-4 was powered by zend engine 1.0 where as PHP-5 was powered by zend engine 2.0

3- In PHP-4 everything is passed by value where as In PHP-5 all objects are passed by refference.

4- PHP-5 introduce a number of new magic function like _CALL, _GET, _SET and _TOSTRING

5- In PHP-5 it is possible you can declare a class as Abstract.

6- PHP-5 introduce a special function called _autoload.

7- PHP-5 removed register_global(), magic quotes and safe mode. 
This was due to the fact that register_global had opened security holes by allowing data injection.

8- PHP-5 introduce a new mysql extention named MYSQLI for developer in Mysql 4.1 and later version.

Happy Coding :)

What is InnoDB storage engine

The InnoDB storage engine provides secure transaction capability. A transaction is a logical groping of statements that is handled by the database server as a single unit.  Either all the statements execute successfully to completion or all modification made by the statements are discarded if an error occurs. 
Transaction system often are described as ACID complient :

ATOMIC - All the statements execute successfully or are canceled as a unit.

CONSISTENT - A database that is in a consistent state when a transaction begins is left in a consistent state by the transaction.

ISOLATED - One transaction does not affected to another transaction.

DURABLE - All the changes made by a transaction that completes successfully are stored properly in the database. Changes are not lost.

Features of InnoDB storage engine:
  1. Provides Full transaction capability with full ACID (Atomic, Consistency, Isolated, and Durabe) compliance.
  2. It has row level locking. By supporting row level locking, you can add data to an InnoDB table without the engine locking the table with each insert and this speeds up both the recovery and storage of information in the database.
  3. The key to the InnoDB system is a database, caching and indexing structure where both indexes and data are cached in memory as well as being stored on disk This enables very fast recovery, and works even on very large data sets.
  4. InnoDB supports foreign key constraints
  5. InnoDB supports automatic crash recovery.
  6. InnoDB supports table compression (read/write)
  7. InnoDB supports spatial data types (no spatial indexes)
  8. Innodb support non-locking ANALYZE TABLE and is only required when the server has been running for a long time since it dives into the index statistics and gets the index information when the table opens.
  9. Innodb does not have separate index files so they do not have to be opened.
  10. Innodb builds its indexes one row at a time in primary key order (after an ALTER), which means index trees aren't built in optimal order and are fragmented.There is currently no way to defragment InnoDB indexes, as 
  11. InnoDB can't build indexes by sorting in MySQL 5.0. Even dropping and recreating InnoDB indexes may result in fragmented indexes, depending on the data.
  12. A table can contain a maximum of 1000 columns.
  13. The InnoDB internal maximum key length is 3500 bytes, but MySQL itself restricts this to 3072 bytes. (1024 bytes for non-64-bit builds before MySQL 5.0.17, and for all builds before 5.0.15.)
  14. The default database page size in InnoDB is 16KB. By recompiling the code, you can set it to values ranging from 8KB to 64KB. You must update the values of UNIV_PAGE_SIZE and UNIV_PAGE_SIZE_SHIFT in the univ.i source file.
  15. InnoDB tables do not support FULLTEXT indexes.

If you like this don't forget to leave a comment below.

Happy Coding:)