CodeFoodPixels / node-promise-mysql

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

createPool is not a function #6

Closed chpio closed 9 years ago

chpio commented 9 years ago

Hi,

it seams like the createPool function is not defined.

CodeFoodPixels commented 9 years ago

As is stated in the readme, only the standard connection is implemented at the moment. I've not had the chance to implement createPool yet.

On Mon, 22 Jun 2015 12:10 pm t128 notifications@github.com wrote:

Hi,

it seams like the createPool function is not defined.

— Reply to this email directly or view it on GitHub https://github.com/lukeb-uk/node-promise-mysql/issues/6.

chpio commented 9 years ago

oki, im using bluebird directly: https://github.com/petkaantonov/bluebird/blame/4576d0b18d5bff5194d74be5b4530e275c12912e/API.md#L1314-L1325

edi commented 8 years ago

Hello! I'm somewhat new to this whole asynchronous thing, but, bumping into callbacks hell lead me here. So, I just noticed that using the latest node package via npm install promise-mysql I'm still having this issue. In the reply above you stated that only the standard connection is implemented as of yet. Given the fact that it was almost a year ago, I have checked the source code and it actually actualy has a createPool at the moment.

Hence my question.. why am I receiving `mysql.createPool(...).then is not a function.

My implementation looks like this ( again, I'm somewhat new so I might be wrong ).

var mysql = require('promise-mysql'),
    connection,
    DB = function() {
        this.pool = mysql.createPool({ host: 'localhost', user: '', password: '', database: '' }).then(function(conn){
            connection = conn;
        })
    };

DB.prototype.query = function( statement, format, callback ) {
    this.pool.getConnection().then(function( connection ){
        return connection.query( statement, format );
    }).catch(function( err ) {
        console.log( err );
    });
}

module.exports = new DB();

I just tried following the README. Is there something I'm doing wrong, I have also ran npm install bluebird.

What I'm trying to achieve is that, given a route.js file for example, being an express app, I want to do the following:

var db = require( './controllers/DB.js' );
db.query( 'SELECT * FROM users WHERE id = ?', [ user.id ] ).then( rows => {
    console.log( rows )
}); // eventuall chain another promise after this one as well if possible.

Please advise on how would be the correct way to do this. Thank you!

CodeFoodPixels commented 8 years ago

You don't need the . then on the createPool call as this returns a pool object and not a promise. You don't need to install bluebird yourself, it's in the dependencies for the module

edi commented 8 years ago

@lukeb-uk Ok buddy, sorry. I did the modifications.

DB.js looks like the following now:

var DB = function() {
    this.pool = require('promise-mysql').createPool({ db: details });
}
DB.prototype.query = function( sql ) {
    this.pool.getConnection().then( conn => {
        return conn.query( sql ); 
                // I see releaseConnection in the docs but I don't see it ever getting called
    }).catch( err => {
        console.log( err );
    });
}
module.exports = new DB();

Consider this to be my simplifed routes module:

var db = require( './controllers/DB.js' );
db.query( 'SELECT * FROM users WHERE id = 1 LIMIT 1' ).then( rows => {
    console.log( rows )
})

The problem now is that while in my opinion it should work perfectly as conn.query returns a Promise, it actually comes up as TypeError: Cannot read property 'then' of undefined

Doing a console.log( result ) in DB.prototype.query method with a fully valid SQL query, the following comes up on the logs:

Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_progressHandler0: undefined,
_promise0: undefined,
_receiver0: undefined,
_settledValue: undefined
}
}

I don't understand what I'm doing wrong... can you shed some light ?

chpio commented 8 years ago

The problem now is that while in my opinion it should work perfectly as conn.query returns a Promise, it actually comes up as TypeError: Cannot read property 'then' of undefined

yeah, you have to return the promise:

DB.prototype.query = function( sql ) {
    return this.pool.getConnection().then( conn => {
        return conn.query( sql ); 
                // I see releaseConnection in the docs but I don't see it ever getting called
    }).catch( err => {
        console.log( err );
    });
}

PS/off topic: also i find async/await much more appealing than normal promises:

async function doAsyncStuff() {
  const rows = await sql.query('SELECT stuff FROM my_table');
  let sum;
  for (const row of rows) {
    sum += row.stuff;
  }
  return sum;
}
edi commented 8 years ago

Ahhh damn, I'm so stupid. I just realized my mindset when it comes to node is trapped into this callback hell. I was returning the query result but not the Promise itself in the query method.

@chpio How to use those ...I get unexpected token function after async..