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.