I'm new to node.js and I have some code that isn't working properly. I think the issue is in asynchronous functions. This code is pretty simple. Read some xml, assign the values to variables, assign those variables as properties that are used to connect to a database in another node.js file. Problem is the variables don't update after reading the xml.
Here's the code,
//set some variables
userXML = "bgdf";
passwordXML = "";
serverXML = "";
databaseXML = "";
//parse the xml
var fs = require('fs'),
xml2js = require('xml2js');
var parser = new xml2js.Parser({explicitArray : false});
fs.readFile(__dirname + '/variables.xml', function(err, data) {
parser.parseString(data, function (err, result) {
userXML = result.variables.user;
passwordXML = result.variables.password;
serverXML = result.variables.server;
databaseXML = result.variables.database;
//What's in this variables? The right values!
console.log(userXML);
console.log(passwordXML);
console.log(serverXML);
console.log(databaseXML);
})
});
exports.dev = {
db: {
//What's in these variables? The wrong values! Values from top of file - never updated
user: userXML,
password: passwordXML,
server: "myservername.net",
database: databaseXML
}
};
I tried adding a .then() promise after the readFile but then my other file is saying that "dev" is undefined.
errvariable(s) for errors?