I need help about this code:
var sqlCheckIfExist = "SELECT my_refer FROM hub_user WHERE my_refer = '" + friendReferCode + "'";
var sqlCodeCheckSameAsMine = "SELECT my_refer FROM hub_user WHERE uid = '" + uid + "'";
async function checkIfUserCodeExist() {
connection.promise().query(sqlCheckIfExist)
.then(([rows, fields]) => {
if (rows == 0) {
console.log("Non esiste!")
return res.send(JSON.stringify({
"status": 500,
"response": "codeNotExist"
}));
}
checkIfCodeIsSameAsMine()
console.log("Esiste!")
console.log(rows[0].my_refer);
})
.catch(console.log)
.then(() => connection.end());
}
async function checkIfCodeIsSameAsMine() {
connection.promise().query(sqlCodeCheckSameAsMine)
.then(([rows, fields]) => {
if (rows == friendReferCode) {
console.log("Codice uguale!")
return res.send(JSON.stringify({
"status": 500,
"response": "sameCodeAsMine"
}));
}
console.log("Codice non uguale!")
})
.catch(console.log)
.then(() => connection.end());
}
checkIfUserCodeExist()
I am establishing the connection in this way:
app.use(function(req, res, next) {
global.connection = mysql.createConnection({
host: 'xx',
user: 'xx',
password: 'xx',
database: 'xx'
});
connection.connect();
next();
});
I can't understand one thing: How can I call nested query? When I check if rows == 0 into checkIfUserCodeExist() function , if its false, I call checkIfCodeIsSameAsMine() but I got this error:
Error: Can't add new command when connection is in closed state
at Connection._addCommandClosedState (/usr/myserver/node_modules/mysql2/lib/connection.js:135:17)
at Connection.end (/usr/myserver/node_modules/mysql2/lib/connection.js:836:26)
at connection.promise.query.then.catch.then (/usr/myserver/addReferFriend.js:45:31)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
How can I fix that thing?
I post the full file here:
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.post('/', function(req, res, next) {
var uid = req.body.uid;
var friendReferCode = req.body.friendReferCode;
var sqlCheckIfExist = "SELECT my_refer FROM hub_user WHERE my_refer = '" + friendReferCode + "'";
var sqlCodeCheckSameAsMine = "SELECT my_refer FROM hub_user WHERE uid = '" + uid + "'";
async function checkIfUserCodeExist() {
connection.promise().query(sqlCheckIfExist)
.then( ([rows,fields]) => {
if (rows == 0) {
console.log("Non esiste!")
return res.send(JSON.stringify({"status": 500,"response": "codeNotExist"}));
}
checkIfCodeIsSameAsMine()
console.log("Esiste!")
console.log(rows[0].my_refer);
})
.catch(console.log)
.then( () => connection.end());
}
async function checkIfCodeIsSameAsMine() {
connection.promise().query(sqlCodeCheckSameAsMine)
.then( ([rows,fields]) => {
if (rows == friendReferCode) {
console.log("Codice uguale!")
return res.send(JSON.stringify({"status": 500,"response": "sameCodeAsMine"}));
}
console.log("Codice non uguale!")
})
.catch(console.log)
.then( () => connection.end());
}
checkIfUserCodeExist()
});
module.exports = router;
Thanks in advance!