0

I am trying to use promise-mysql to do queries to the db. But for some reason I get an error

con.query is not a function

In my IDE this function has tab-completion, but for some reason when I run this piece of code it doesn't recognize the functions existence. But it does recognize the function getConnection.

My code took inspiration from the accepted answer here: Node.js MySQL Error Handling

Below is relevant code.

const pool = require('../pool')
...
...
router.post('/', async (req,res) => {   
    let navn = req.body.navn;
    let query = `SELECT * FROM signups WHERE navn LIKE ('${navn}');`
    console.log(query)

    //get connection from connection pool, execute query, release connection back to pool
    try {
        const con = (await pool).getConnection()
        console.log("got pool connection")
        try {
            const result = await con.query(query)
        } catch(err) {
            console.log("Could not resolve")
            res.status(HttpStatus.StatusCodes.INTERNAL_SERVER_ERROR).send({ error: err, message: err.message });
        }
    } catch (err) {
        res.status(HttpStatus.StatusCodes.INTERNAL_SERVER_ERROR).send({ error: err, message: err.message });
    }
}
//pool.js
'use strict';

const mysql = require('promise-mysql');

const pool = mysql.createPool({
  connectionLimit: 20,
  host: 'localhost',
  user: 'root',
  password: 's2gftw!!!!!!!!',
  database: 'signups',
});

module.exports = pool;

I would like to get why this error occurs.

Edit: yes I know this code is suceptable to SQLi, but that is the purpose for now.

Mr Krisey
  • 109
  • 2
  • 10
  • The application is supposed to be hackable. So its not for production. That is why there is an SQLi there. I could have used prepared statement, but I did not do it on purpose. I don't use this password elsewhere ;) – Mr Krisey Jul 04 '21 at 01:16
  • 1
    I was only remarking in the number of exclamation points :D – Mulan Jul 04 '21 at 14:06

0 Answers0