Sending an email using NodeMailer & Gmail

Source: https://codeburst.io/sending-an-email-using-nodemailer-gmail-7cfa0712a799

inside your node.js file write
var transporter = nodemailer.createTransport({
 service: 'gmail',
 auth: {
        user: 'youremail@address.com',
        pass: 'yourpassword'
    }
});
We used gmail as our transport service.

How do I do a bulk insert in mySQL using node.js

Source: https://stackoverflow.com/questions/8899802/how-do-i-do-a-bulk-insert-in-mysql-using-node-js


Bulk inserts are possible by using nested array, see the github page
Nested arrays are turned into grouped lists (for bulk inserts), e.g. [['a', 'b'], ['c', 'd']] turns into ('a', 'b'), ('c', 'd')
You just insert a nested array of elements.
An example is given in here
var mysql = require('node-mysql');
var conn = mysql.createConnection({
    ...
});

var sql = "INSERT INTO Test (name, email, n) VALUES ?";
var values = [
    ['demian', 'demian@gmail.com', 1],
    ['john', 'john@gmail.com', 2],
    ['mark', 'mark@gmail.com', 3],
    ['pete', 'pete@gmail.com', 4]
];
conn.query(sql, [values], function(err) {
    if (err) throw err;
    conn.end();
});
Note: values is an array of arrays wrapped in an array
[ [ [...], [...], [...] ] ]

Angular material Autocomplete: select option

Source: https://stackoverflow.com/questions/42427928/material-2-autocomplete-select-option

The Material Autocomplete component has its own optionSelected event output:
Template:
<mat-autocomplete (optionSelected)="onSelectionChanged($event)"> 
  <mat-option *ngFor="let item of myItems" [value]="item">
    {{ item }}
  </mat-option>
</mat-autocomplete>
Controller:
import { MatAutocompleteSelectedEvent } from '@angular/material';

// ...

onSelectionChanged(event: MatAutocompleteSelectedEvent) {
  console.log(event.option.value);
}

Node.js MySQL Multiple Statement Queries

Source: https://www.technicalkeeda.com/nodejs-tutorials/nodejs-mysql-multiple-statement-queries


  1. var mysql = require('mysql');
  2.  
  3. var connection = mysql.createConnection({
  4. host: 'localhost',
  5. user: 'root',
  6. password: '',
  7. database: 'technicalkeeda',
  8. debug: false,
  9. multipleStatements: true
  10. });
  11. connection.connect();
  12.  
  13. var sql = "SELECT * FROM trn_employee WHERE employee_id = ?;SELECT * FROM trn_person WHERE person_id = ?";
  14.  
  15. connection.query(sql, [2, 1], function(error, results, fields) {
  16. if (error) {
  17. throw error;
  18. }
  19. console.log(results[0]);
  20. console.log(results[1]);
  21. });
  22.  
  23. connection.end();

  1. [ { employee_id: 2, first_name: 'Dinesh', last_name: 'Patil' } ]
  2. [ { person_id: 1, first_name: 'Yashwant', last_name: 'Chavan', age: 10 } ]

how to show autocomplete options on focus

Source: https://stackoverflow.com/questions/52536382/how-to-show-autocomplete-options-on-focus


the filter is executed when the page loads.. but I loaded the data on graphql so the data arrived after the first filter executed. I changed it so the filter will be executed only after the data was received.
thanks Swoox for helping me notice it.
ngOnInit() {
 ...
 this.carsService.GetCarCompanies().subscribe((data: any) => {
      this.carCompanies = [];
      this.carCompaniesLowercase = [];
      data.data.car_companies.forEach((row) => {
        this.carCompanies.push(row.company_name);
        this.carCompaniesLowercase.push(row.company_name.toLowerCase());
      });
      this.filteredCarCompanies = this.carTypeFormGroup.get('carCompany').valueChanges
        .pipe(startWith(''), map(carCompany => carCompany ? this._filterCarCompanies(carCompany) : this.carCompanies.slice()));
    });

Moment duration

Source: https://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes/34672015

moment.duration(now.diff(then)).humanize()


public getDurationText(date1, date2) {
if (!date1) {
return '';
}
// check null date2 (is current date)
const date2Tmp = date2 ? date2 : new Date();
// return
return moment.duration(moment(date2Tmp).diff(moment(date1))).humanize();
}

Mysql: Sort In Descending Order With NULLs First

Source: https://www.designcise.com/web/tutorial/how-to-order-null-values-first-or-last-in-mysql

Sort In Descending Order With NULLs First

Using The IS NOT NULL Comparison Operator:

Similar to the IS NULL operator, we can rely on the fact that IS NOT NULLreturns 1 when expression is NOT NULL, and 0 otherwise.
Query:
SELECT * FROM user 

ORDER BY date_login IS NOT NULL, date_login DESC
Using !ISNULL() is equivalent to IS NOT NULL, so either one could be used.
The same query could also be rewritten using IS NULL like so:
SELECT * FROM user 

ORDER BY date_login IS NULL DESC, date_login DESC
Expected Result:
+----+--------+------------+
| id |  name  | date_login |
+----+--------+------------+
|  2 |  john  | NULL       |
|  1 |  NULL  | 2017-03-12 |
|  4 |  zayne | 2017-03-02 |
|  3 |  david | 2016-12-24 |
+----+--------+------------+

Moment: How to prevent “Invalid date”?

Source: https://stackoverflow.com/questions/28993107/momentjs-how-to-prevent-invalid-date


If your goal is to find out whether you have a valid date, use Moment's isValid:
var end_date_moment, end_date;
jsonNC.end_date = jsonNC.end_date.replace(" ", "T");
end_date_moment = moment(jsonNC.end_date);
end_date = end_date_moment.isValid() ? end_date_moment.format("L") : "";
...which will use "" for the end_date string if the date is invalid.

Cold Turkey Blocker

 https://superuser.com/questions/1366153/how-to-get-rid-of-cold-turkey-website-blocker-get-around-the-block Very old question, but still wan...