I am trying to use the data from an express request to access a mysql database to use as a response. But am struggling to get the data back using promises.
Express bit
app.get('/loadDrinks', function(req, res) {
var q = 'SELECT * FROM `Drinks` WHERE `Name` LIKE "%' + req.query.drinks + '%"';
mysqlQ(q).then(function() {
console.log(data);
res.send(" ");
})
})
The mysql bit I am using promise-mysql and bluebird to handle the promises
function mysqlQ(q) {
mysql.createConnection({
host: 'localhost',
user: 'correctUser',
password: 'correctPass',
database: 'correctDB'
}).then(function(conn) {
connection = conn;
var f = connection.query(q);
//connection.end()
return f
}).then(function (rows) {
return rows;
})
}
I am getting the error:
TypeError: Cannot read property 'then' of undefined
on Line 67:14 which is
mysqlQ(q).then(function() {
But mysqlQ
should return the value of Rows
Your endpoint definition lacks the data
parameter inside then
app.get('/loadDrinks', function(req, res) {
var q = 'SELECT * FROM `Drinks` WHERE `Name` LIKE "%' + req.query.drinks + '%"';
// here you had function() - without data parameter which you try to log further
mysqlQ(q).then(function(data) {
console.log(data);
res.send(" ");
});
});
And you forgot the return
statement in mysqlQ
function (your function did not return anything and it was the reason of the TypeError
about undefined
)
function mysqlQ(q) {
return mysql.createConnection({
...