I've been having trouble learning callbacks and promises coming from a synchronous programming language. I've been trying to get the result of the .env
file, so then I can use it in my application but instead console.log(envdata)
returns:
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_progressHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_settledValue: undefined
}
In my console. How do I instead get the correct results from the file?
Here is my current code.
'use strict';
var envdata = {};
var path = require('path');
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
module.exports = function(app, options) {
return new Promise(function(resolve, reject){
envdata = getenvdata(options.basedir + path.sep + ".env");
console.log(envdata); //need this to be the data
app.envdata = envdata;
resolve(app);
});
function getenvdata(path){
return fs.readFileAsync(path, 'utf8')
.then(function(data){
data.split("\n").forEach(function(keyvalue) {
var array = keyvalue.split('=');
array[1] && (data[array[0]] = array[1]);
});
return data;
});
}
}