数据未被推入 mysql table
the data is not pushed into mysql table
connection.query("SELECT email, password FROM authenticate where email=?", email,
function (error, result) {
if (error) {
throw error;
} else {
if (result.length == 0) {
console.log('name not available');
res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
} else if (result[0].password == password) {
console.log(userData);
connection.query("INSERT INTO userapplications SET ? ", userData,
function (error, result, fields) {
if (error){
throw error;
}
else{
console.warn("insertion successful");
res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
}
});
} else {
console.log('invalid user and password');
res.sendFile(path.join(__dirname + "/public/apply.html"))
}
}
});
connection.end();
错误:
Cannot enqueue Query after invoking quit.
由于某种原因,数据库已连接,数据未添加到 table。
我是初学者。
我认为这是因为 NodeJS 的异步特性。尝试将 connection.end() 与 connection.query() 连接起来。
喜欢:
connection.query("your_code_here).end();
像下面这样移动你的 connection.end();
内部回调。
connection.query("SELECT email, password FROM authenticate where email=?", email,
function (error, result) {
if (error) {
connection.end();
throw error;
} else {
if (result.length == 0) {
console.log('name not available');
res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
} else if (result[0].password == password) {
console.log(userData);
connection.query("INSERT INTO userapplications SET ? ", userData,
function (error, result, fields) {
if (error){
connection.end();
throw error;
}
else{
console.warn("insertion successful");
res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
connection.end();
}
});
} else {
console.log('invalid user and password');
res.sendFile(path.join(__dirname + "/public/apply.html"))
connection.end();
}
}
});
connection.end();
类似这样,你可以修改。
connection.query("SELECT email, password FROM authenticate where email=?", email,
function (error, result) {
if (error) {
throw error;
} else {
if (result.length == 0) {
console.log('name not available');
res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
} else if (result[0].password == password) {
console.log(userData);
connection.query("INSERT INTO userapplications SET ? ", userData,
function (error, result, fields) {
if (error){
throw error;
}
else{
console.warn("insertion successful");
res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
}
});
} else {
console.log('invalid user and password');
res.sendFile(path.join(__dirname + "/public/apply.html"))
}
}
});
connection.end();
Cannot enqueue Query after invoking quit.
由于某种原因,数据库已连接,数据未添加到 table。 我是初学者。
我认为这是因为 NodeJS 的异步特性。尝试将 connection.end() 与 connection.query() 连接起来。
喜欢:
connection.query("your_code_here).end();
像下面这样移动你的 connection.end();
内部回调。
connection.query("SELECT email, password FROM authenticate where email=?", email,
function (error, result) {
if (error) {
connection.end();
throw error;
} else {
if (result.length == 0) {
console.log('name not available');
res.sendFile(path.join(__dirname + "/public/applyFailure.html"));
} else if (result[0].password == password) {
console.log(userData);
connection.query("INSERT INTO userapplications SET ? ", userData,
function (error, result, fields) {
if (error){
connection.end();
throw error;
}
else{
console.warn("insertion successful");
res.sendFile(path.join(__dirname + "/public/applySuccess.html"));
connection.end();
}
});
} else {
console.log('invalid user and password');
res.sendFile(path.join(__dirname + "/public/apply.html"))
connection.end();
}
}
});
connection.end();
类似这样,你可以修改。