0

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.

FallUp
  • 1
  • 1
  • I guess [this](http://stackoverflow.com/q/14220321/1048572?how-to-return-the-response-from-an-asynchronous-call) is your actual problem. You can't do that. `connect` is asynchronous, and you can't know whether it had a problem before it finished. – Bergi Feb 26 '16 at 14:26

1 Answers1

1

You're having lexical scoping issues with 'this'. In the callback function you are passing to your connect method 'this' is scoped to the current function you are executing and not the object you are building. I've re-structured your code so that you can be able to access the property you are trying to reach... also restructured for proper prototypical inheritance, eliminating the need for the extend method... added an example underneath

see this thread: https://stackoverflow.com/a/32025281/2665925

reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create

var underscore = require('underscore');
var mysql = require('mysql');
var baseConfig = require('../config/base');

var autoload = require('../autoload');

var core = autoload.load(baseConfig.dirs.core);

var dbConfigObj = {
        host: 'localhost',
        user: 'user',
        password: '12345',
        database: 'CarDB'
    }

function DBConnector(config, res){
    var objectScope = this;

    var connection = mysql.createConnection(config);

    connection.connect(function(error) {
        if(error) objectScope.error = error;
    });

    this.database = connection;
    if(objectScope.error) return false;    

}

DBConnector.prototype = {
    name: 'model',
    database: null,
    error: null,

    extend: function(child) {
        return underscore.extend(this, child);
    }
}

DBConnector.prototype.constructor = DBConnector; 

function CarModel(){
    DBConnector.call(this, dbConfigObj);
}

CarModel.prototype = Object.create(DBConnector.prototype,{
    color: {
        value: null //default value,
        writable: true,  
        configurable:true,
    }
})

CarModel.prototype.constructor = CarModel

module.exports = CarModel
Community
  • 1
  • 1
parallaxis
  • 196
  • 1
  • 13