我需要有关此代码的帮助:
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()
我是这样建立联系的:
app.use(function(req, res, next) {
global.connection = mysql.createConnection({
host: 'xx',
user: 'xx',
password: 'xx',
database: 'xx'
});
connection.connect();
next();
});
我不明白一件事:如何调用嵌套查询?在checkifusercodeexist()函数中检查rows==0时,如果为false,则调用checkifcodeissameasmine(),但出现以下错误:
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)
我怎么能修好那东西?
我把完整的文件贴在这里:
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;
提前谢谢!
3条答案
按热度按时间7cjasjjr1#
这可能是因为您在checkifusercodeexist函数的末尾终止了连接。删除以下行,我认为它会起作用:
或者,每次创建方法时都要打开和关闭的连接将返回新连接并在进行任何db查询之前调用它。例子:
mrwjdhj32#
好吧,你的代码有多个问题。我将从解决您的具体问题开始,然后给出一些额外的提示。:)
你的问题就在于这个逻辑:
这个
checkIfCodeIsSameAsMine()
函数是异步的。所以,在这个代码链中发生的是checkIfCodeIsSameAsMine()
但你不要等待它的结果,并立即跳到最后then()
关闭数据库连接。因此,本质上,在checkIfCodeIsSameAsMine()
正在关闭连接后执行。你应该
return checkIfCodeIsSameAsMine()
. 这样你就可以等待Promise
该功能的响应。现在我要补充一点。
第一,
"SELECT my_refer FROM hub_user WHERE uid = '" + uid + "'";
很糟糕。将应用程序暴露于sql注入等漏洞。您应该通过一些解析功能来转义sql查询中的动态值。这通常是通过orm(即connection()
你用的)。第二,如果你使用
async
然后函数使用相应的await
功能。这样地:pod7payv3#
您的程序中有多个问题需要更新。
首先,不能使用全局变量来存储每个请求的数据库连接。如果两个请求同时到达,那么一个请求将覆盖
connection
因此,您可能会对两个请求使用相同的连接,和/或不关闭其中一个连接,从而导致挂起连接,在最坏的情况下,这可能会导致应用程序无响应。要解决此问题,必须传递与请求对象的连接:
要访问按请求定义的连接,您可以执行以下操作:
function checkIfUserCodeExist(connection) {
return connection.query(sqlCheckIfExist)
.then(([rows, fields]) => {
if (rows == 0) {
console.log("Non esiste!")
}
function checkIfCodeIsSameAsMine(connection) {
return connection.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!")
})
}
async function checkIfUserCodeExist(connection) {
let [rows, fields] = await connection.query(sqlCheckIfExist)
if (rows == 0) {
console.log("Non esiste!")
return res.send(JSON.stringify({
"status": 500,
"response": "codeNotExist"
}));
}
await checkIfCodeIsSameAsMine()
console.log("Esiste!")
console.log(rows[0].my_refer);
}
async function checkIfCodeIsSameAsMine(connection) {
let [rows, fields] = await connection.query(sqlCodeCheckSameAsMine)
if (rows == friendReferCode) {
console.log("Codice uguale!")
return res.send(JSON.stringify({
"status": 500,
"response": "sameCodeAsMine"
}));
}
console.log("Codice non uguale!")
}
return res.send(JSON.stringify({
"status": 500,
"response": "codeNotExist"
}));
throw new CustomError(500, "codeNotExist")
app.use(function(err, req, res, next) {
return res.send({
"status": err.status,
"response": err.message
});
})
/* 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 + "'";
function checkIfUserCodeExist() {
return req.dbConnection.query(sqlCheckIfExist)
.then(([rows, fields]) => {
if (rows == 0) {
console.log("Non esiste!")
}
function checkIfCodeIsSameAsMine() {
return req.dbConnection.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!")
})
}
checkIfUserCodeExist()
.catch(next)
});