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