Lessons Learned #2

Aug 10, 2017

New project, should I use ES 6 or ES 5.1?

So, despite all the red showing that ES 6 isn't supported by older browsers 
Like here: or here or here. I decided to go with ES6 for a new project. 

I mean, ES5 was standardized in 2009. 8 years ago. According to Moore's law, that's like 6 generations, or roughly the equivalent of 120 human years. Best not to start with something that ancient!  I'm going with ES 6. 

Sigh ... well, one drawback is that my muscle memory types 'var' when now I guess I should be using 'let' ...😆

More promises and better ways

I've got a lot to learn. But I found this, it was clear and easy to understand. Kudos to the folks at Tao of Code

Requiring and importing code ... yeah parentheses matter!


I started with this
database.js
module.exports.database = function () {
    return mysql.createPool(config.database);};

And used it like this
let Database = require ('./database').database();

But I don't like the .database(); on the end.
To fix that, I changed it to this

database.js
module.exports = function () {
    return mysql.createPool(config.database);}();

And use it like this.
let Database = require ('./database');

Little things, but those parentheses really matter. I had to make sure to export the results of the executed function, not the function...

Lessons Learned #1

Aug 7, 2017

So, the purpose of this series of posts, is to just talk about the lessons learned each day.  We discover and uncover all kinds of things that either make our work easier, or have been making our life difficult.  The goal here is to codify our experience so that we can at least remember what we learned, and perhaps help our fellow man if they come across the same problem.

Of exports =  vs. module.exports = ???

Basically, exports is a reference to module.exports.  Meaning, if you change module.exports, then exports is changed too. In contrast if you change exports to something else, then module.exports and exports are now pointing at two separate things.

I choose to use module.exports =
Just so I don't have to remember that those two are different.

Source


Of Promises and Promise chains not being run sequentially

So, this one threw me for a loop for longer than I care to admit.

I was testing promises and this did not work, the getAlls were being run out of sequence 

print()
    .then(contact.truncate())
    .then(contact.getAll())
    .then(contact.getAll())
    .then(print())
    .then(print())
    .then(print())
    .then(function(){
        done();
    });

The problem was that I had the '()' on the end of the function. When I removed it, from each line, sure enough, it worked. The reason for this (I think) is that above, contact.getAll() would be the actual promise, not the function that returns a promise, whereas below, contact.getAll is the function that will return a promise.


print()
    .then(contact.truncate)
    .then(contact.getAll)
    .then(contact.getAll)
    .then(print)
    .then(print)
    .then(print)
    .then(function(){
        done();
    });

Anyway, it took too long to figure out, hopefully it helps someone.

More promises and parameters

This worked
return new Promise (function (fulfill, reject) {
    runQuery(query, values).then(function (results1) {
        self.getOne(obj.id).then(function(results) {
            fulfill(results);
        });
    })
})

This did not work
return new Promise (function (fulfill, reject) {
    runQuery(query, values)
       .then(self.getOne(obj.id))
       .then(function(results) {
            fulfill(results);
        });
    })
})

In the first case, I got what I wanted, which was the result of self.getOne.  In the second case, fulfill(results) sent the results from runQuery.  I didn't expect that...It is because I passed in self.getOne(obj.id) with parameters, which, processes the function before sending it.  I'll get it right eventually!

Bodyparser settings?

I had this error

body-parser deprecated bodyParser: use individual json/urlencoded middlewares node_modules/express/lib/router/layer.js:95:5

express deprecated req.host: Use req.hostname instead node_modules/body-parser/index.js:100:29
body-parser deprecated undefined extended: provide extended option node_modules/body-parser/index.js:105:29

when my code looked like:

var bodyParser = require('body-parser');
app.use(bodyParser);

I needed to switch to look like:
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json);

Then everything worked fine.

Angular pretty URL 

So, default angular.js (1.5.3) has a # in the url. You can mitigate that pretty easy to make pretty urls by adding <base href="/"> inside the <head> element and after the <meta> tags. Also, in the angular_routes.js file add the '$locationProvider.html5Mode(true);'
angular.module('myapp')
    .config (['$routeProvider', '$locationProvider',
    function ($routeProvider, $locationProvider) {
        $routeProvider
            .when('/home', {
                template: ''
            })
        $locationProvider.html5Mode(true);
     }
]);

Node.js api routes not including the body?

I found that the reason my body parameters was empty was because I had called the appropriate middleware functions in the wrong order in the node.js express server.

For example, this left the body empty
var express = require('express');
var app = express();
var routesApi = require('./routes/routes');
var bodyParser = require('body-parser');

app.use('/api', routesApi);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

And this worked
var express = require('express');
var app = express();
var routesApi = require('./routes/routes');
var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/api', routesApi);

Sources







XAMPP phpMyAdmin subdomain on localhost

Create a localhost subdomain for phpMyAdmin on XAMPP Open xampp\apache\conf\extra\http-vhosts.conf and append the following (modify the Docu...