Hello,
I'm learning programming on NodeJS, it's my first back-end experience.
I have a small problem. I created class (module) Database, which manages
connection and queries to a database, here is implementation:
var mysql = require('mysql');
function Database() {
this.connection = null;
* this.connected = false;*
}
Database.prototype.connect = function (config, callback) {
console.log('[Database] Connecting...');
this.connection = mysql.createConnection(config);
this.connection.connect(function (err) {
if (err == null) {
* this.connected = true;*
}
console.log('connect: this.connected = ' + this.connected);
console.log('[Database] Connected');
callback(err);
});
}
Database.prototype.makeQuery = function (query, callback) {
console.log('makeQuery: this.connection = ' + this.connection);
console.log('makeQuery: this.connected = ' + this.connected);
if (this.connected == false) {
* return callback(new Error('[Database] You cannot make queries
before connection'));*
}
console.log('[Database] Making query: %s', query.sql());
* // making query to the database*
}
exports.Database = Database;
As you can see, here I store *connected* property. If I try to make query
before successful connection I will get error *[Database] You cannot make
queries before connection*. But it doesn't work, it shows me this error
always, because *connected* property becomes false, here is example of
usage this module outside:
var config = require('./config.json');
var query = require('./modules/database/query');
console.log('Server Started');
var database = new (require('./modules/database')).Database();
database.connect(config.database, function (err) {
console.log("connect handler");
if (err) {
throw err;
}
var query = new query.Query();
* // some actions with query, doesn't matter*
database.makeQuery(categoriesNameQuery, function (err, result) {
if (err) {
throw err;
}
* // working with the result*
});
});
In console I get the following:
/usr/local/bin/node database_test.js
Server Started
[Database] Connecting...
*connect: this.connected = true*
[Database] Connected
connect handler
makeQuery: this.connection = [object Object]
*makeQuery: this.connected = false*
*Error: [Database] You cannot make queries before connection*
at Database.makeQuery (/modules/database/index.js:35:25)
at /database_test.js:30:14
...
I can't understand, why property *connected* is *true* after connection,
and *false* when I make a query. But property *connection* works fine!
Maybe enclosures in NodeJS work by other way.
Thank you for any help.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.