最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pool.query is not a function - Using npm mysql package to use connection pool - Stack Overflow

programmeradmin0浏览0评论

I'm using npm mysql package, to create a connection pool to my mysql DataBase with dbHandler.js :

var mysql = require("mysql");

var pool = mysql.createPool({
    connectionLimit : 10,
    host: "path.to.host",
    user: "username",
    password: "password",
    database: "dbname",
    debug: false
});

exports.pool = pool;

Then I call it inside routesHandler.js, to handle my SQL request :

var mysql = require("mysql");
var pool = require("./dbHandler");

exports.login = function(req, res) {
    var email = req.body.email;
    pool.query('SELECT * FROM users WHERE users.email = ?', [email], function(
        error,
        results,
        fields
    ) {
        if (error) {
            res.send({
                code: 400,
                failed: "Error ocurred"
            });
        } else {
            if (results.length > 0) {
                //Mail authorized
                res.send({
                    code: 200,
                    success: "Sucessfull login"
                });
            } else {
                //Mail unauthorized
                res.send({
                    code: 403,
                    failed: "Unauthorized mail"
                });
            }
        }
    });
};

I'm not sure it's the best way to do it, but I can't seem to found a proper example... :/
I would gladly :) accept any suggestions or links about the way to handle the whole :

  • Connections to database +
  • SQL requests to database

But I'm getting that basic error message but I can't figure out how to get rid of it :

TypeError: pool.query is not a function

Any pointers will be welcome!

I'm using npm mysql package, to create a connection pool to my mysql DataBase with dbHandler.js :

var mysql = require("mysql");

var pool = mysql.createPool({
    connectionLimit : 10,
    host: "path.to.host",
    user: "username",
    password: "password",
    database: "dbname",
    debug: false
});

exports.pool = pool;

Then I call it inside routesHandler.js, to handle my SQL request :

var mysql = require("mysql");
var pool = require("./dbHandler");

exports.login = function(req, res) {
    var email = req.body.email;
    pool.query('SELECT * FROM users WHERE users.email = ?', [email], function(
        error,
        results,
        fields
    ) {
        if (error) {
            res.send({
                code: 400,
                failed: "Error ocurred"
            });
        } else {
            if (results.length > 0) {
                //Mail authorized
                res.send({
                    code: 200,
                    success: "Sucessfull login"
                });
            } else {
                //Mail unauthorized
                res.send({
                    code: 403,
                    failed: "Unauthorized mail"
                });
            }
        }
    });
};

I'm not sure it's the best way to do it, but I can't seem to found a proper example... :/
I would gladly :) accept any suggestions or links about the way to handle the whole :

  • Connections to database +
  • SQL requests to database

But I'm getting that basic error message but I can't figure out how to get rid of it :

TypeError: pool.query is not a function

Any pointers will be welcome!

Share Improve this question asked Nov 24, 2017 at 21:35 R3uKR3uK 14.5k8 gold badges46 silver badges80 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

You do exports.pool = pool;, so you must call the pool propriety like this var pool = require("./dbHandler");.

If you use module.exports=pool in your dbHandler then your code will work as is.

If not just use my answer :

var pool = require("./dbHandler").pool;

exports.login = function(req, res) {
    var email = req.body.email;
    pool.query('SELECT * FROM users WHERE users.email = ?', [email], function(
        error,
        results,
        fields
    ) {
        if (error) {
            res.send({
                code: 400,
                failed: "Error ocurred"
            });
        } else {
            if (results.length > 0) {
                //Mail authorized
                res.send({
                    code: 200,
                    success: "Sucessfull login"
                });
            } else {
                //Mail unauthorized
                res.send({
                    code: 403,
                    failed: "Unauthorized mail"
                });
            }
        }
    });
};

I had a similar problem and I tried most of the available solution but none of them are working and finally, I got the answer to my problem.

dbHandler.js file:

var Pool = require('pg').Pool;

var pool = new Pool({
    user: 'postgres',
    host: 'localhost',
    database: 'database_name',
    password: '#############',
    port: 5432,
});


module.exports = {pool};

anotherfile.js:

let {pool: pool} = require('dbHandler');
// by this calling method I was able to use pool

i think you must use async/await before the function like this :

exports.login = async function(req, res) {
    var email = req.body.email;
   await pool.query('SELECT * FROM users WHERE users.email = ?', [email], function(
        error,
        results,
        fields
    ) {
        if (error) {
            res.send({
                code: 400,
                failed: "Error ocurred"
            });
        } else {
            if (results.length > 0) {
                //Mail authorized
                res.send({
                    code: 200,
                    success: "Sucessfull login"
                });
            } else {
                //Mail unauthorized
                res.send({
                    code: 403,
                    failed: "Unauthorized mail"
                });
            }
        }
    });
};

You have to create a client from the pool, call something like this

const { client } = pool();

await pool.query('SELECT * FROM users WHERE users.email = ?', [email], function(error, results, fields) {
    if (error) {
        res.send({
            code: 400,
            failed: "Error ocurred"
        });
    } else {
        if (results.length > 0) {
            //Mail authorized
            res.send({
                code: 200,
                success: "Sucessfull login"
            });
        } else {
            //Mail unauthorized
            res.send({
                code: 403,
                failed: "Unauthorized mail"
            });
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论