5

first, i connect the db and select DB:

var defaultOptions = {
    user: "root",
    pwd:'admin',
    db:"britcham_dev_local",
    server:"local", // Maybe we don't need this variable.
};

var client = new Client();
client.user = defaultOptions.user;
client.password = defaultOptions.pwd;
client.connect(function (error, results) {
   // 
});
client.query('USE ' + defaultOptions.db, function (error, results) {
   // 
});

Second, I query with client object:

var self = this;
var this.users;
client.query("SELECT * FROM users", function (error, results, fields) {
    if (error) {
        //
    }
    if (results.length  > 0) {
        self.users = results;
    }
});

console.log(this.users);

it's nothing output ??? Why ??

1
  • 1
    Do you receive error when connecting and selecting a DB? Commented May 11, 2011 at 13:57

2 Answers 2

7

Since node.js is non-blocking and asynchronous, then in this code:

client.query("SELECT * FROM users", function (error, results, fields) {
    if (error) {
        //
    }
    if (results.length  > 0) {
        self.users = results;
    }
});

console.log(this.users);

data from DB are not probably loaded yet into users variable when you are trying to log it into console. You can check it out if you do your console.log operation within the query, for example:

client.query("SELECT * FROM users", function (error, results, fields) {
    if (error) {
        //
    }
    if (results.length  > 0) {
        console.log(results);
    }
});

To pass the result into a variable when the operation is finished you can wrap your client DB call into a function with callback parameter and set your variable when the callback is invoked, for example:

function query(sql, callback) {
    client.query(sql, function (error, results, fields) {
        if (error) {
            //
        }
        if (results.length  > 0) {
            callback(results);
        }
    });
}

query("SELECT * FROM users", function(results) {
    self.users = results;
    console.log(self.users);
});

Above code is just a concept.

Sign up to request clarification or add additional context in comments.

3 Comments

A minor comment, but you say that "data from DB are not probably loaded...". The data will actually never be there, right? (since there's only one thread of execution in node and that thread won't be available to execute the callback until after console.log(this.users) has been called)
@Geoff: Data will be there after they are fetched from DB, but at the time when console.log is called (in original code) the users variable is still empty. If the console.log operation is wrapped within setTimeout for example, then users should contain results from DB.
Yeah, sorry, didn't mean to imply never, ever -- just that it will never be there before the console.log call. In other words it's not a race condition (since there's only one thread).
0

How is the suggested answer different from this?

var self = this;
var this.users;
client.query("SELECT * FROM users", function (error, results, fields) {
    if (error) {
    //
    }
    if (results.length  > 0) {
        self.users = results;
        console.log(this.users);
    }
});

I might be wrong this is not different from the suggested answer in that it writes to console no sooner than when we have the data back from the DB.

The suggested answer seems only to add yet another function?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.