CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 63 forks source link

Documentation for setting up connection confusing #20

Closed jmlsf closed 7 years ago

jmlsf commented 8 years ago

I'm not an expert with promises, so I am probably missing something. But as I read the documentation, to me it suggests that you can do something like this:

var mysql = require('promise-mysql');
var connection;

mysql.createConnection({
    host: 'localhost',
    user: 'sauron',
    password: 'theonetruering',
    database: 'mordor'
}).then(function(conn){
    connection = conn;
});
connection.query('select `name` from hobbits').then(function(rows){
    // Logs out a list of hobbits
    console.log(rows);
});

But of course, this doesn't work because connection is undefined when the above code calls connection.query and therefore has no query property yet as we haven't released execution back to the event loop. Instead of doing the connection = conn assignment, I just passed conn to a function that does my database work. I don't see how the method suggested in the documentation could ever work, but maybe I'm missing something. Maybe some clarification for new Promise users might help here?

CodeFoodPixels commented 8 years ago

I'll see how I can clarify it

On Fri, 22 Jul 2016, 12:07 a.m. jmlsf, notifications@github.com wrote:

I'm not an expert with promises, so I am probably missing something. But as I read the documentation, to me it suggests that you can do something like this:

var mysql = require('promise-mysql'); var connection;

mysql.createConnection({ host: 'localhost', user: 'sauron', password: 'theonetruering', database: 'mordor' }).then(function(conn){ connection = conn; }); connection.query('select name from hobbits').then(function(rows){ // Logs out a list of hobbits console.log(rows); });

But of course, this doesn't work because connection is undefined when the above code calls connection.query and therefore has no query property yet as we haven't released execution back to the event loop. Instead of doing the connection = conn assignment, I just passed conn to a function that does my database work. I don't see how the method suggested in the documentation could ever work, but maybe I'm missing something. Maybe some clarification for new Promise users might help here?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lukeb-uk/node-promise-mysql/issues/20, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjy1Ka3EixFbV6GHRkVWsOOLKbmzAaZks5qX_u9gaJpZM4JSTWu .

CodeFoodPixels commented 8 years ago

This is still on my list of things to do. Hopefully I'll get to have a look this week

On Fri, 22 Jul 2016 at 06:07 Luke Bonaccorsi luke@lukeb.co.uk wrote:

I'll see how I can clarify it

On Fri, 22 Jul 2016, 12:07 a.m. jmlsf, notifications@github.com wrote:

I'm not an expert with promises, so I am probably missing something. But as I read the documentation, to me it suggests that you can do something like this:

var mysql = require('promise-mysql'); var connection;

mysql.createConnection({ host: 'localhost', user: 'sauron', password: 'theonetruering', database: 'mordor' }).then(function(conn){ connection = conn; }); connection.query('select name from hobbits').then(function(rows){ // Logs out a list of hobbits console.log(rows); });

But of course, this doesn't work because connection is undefined when the above code calls connection.query and therefore has no query property yet as we haven't released execution back to the event loop. Instead of doing the connection = conn assignment, I just passed conn to a function that does my database work. I don't see how the method suggested in the documentation could ever work, but maybe I'm missing something. Maybe some clarification for new Promise users might help here?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lukeb-uk/node-promise-mysql/issues/20, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjy1Ka3EixFbV6GHRkVWsOOLKbmzAaZks5qX_u9gaJpZM4JSTWu .

CodeFoodPixels commented 7 years ago

Hopefully they're clearer now.