In my Node.js script I need to access an object's property (here the property 'error' of the 'model' object) from a callback. I saw it was impossible to edit this property, but I need it to throw an error and stop the script (if the 'error' property has a value, the 'construct' function returns false, the error is thrown in another part of the script).
var underscore = require('underscore');
var mysql = require('mysql');
var baseConfig = require('../config/base');
var autoload = require('../autoload');
var core = autoload.load(baseConfig.dirs.core);
module.exports = {
name: 'model',
database: null,
error: null,
construct: function(host, user, password, database, res) {
var connection = mysql.createConnection({
host: host,
user: user,
password: password,
database: database
});
connection.connect(function(error) {
if(error) this.error = error;
});
this.database = connection;
if(this.error) return false;
},
extend: function(child) {
return underscore.extend(this, child);
}
}
I would really like to know how to edit this property, because I found no solution yet. Thank you very much.