admin管理员组文章数量:1289603
I've been trying to use async/await with MySQL in node but it returns an undefined value each time. Is there a reason why? Please find my code below.
const mysql = require('promise-mysql');
var connection;
const dbConfig = {
host: "hostname",
database: "dbname",
user: "username",
password: "passwords"
};
async function getResult(){
await mysql.createConnection(dbConfig).then(function(conn){
connection = conn;
var result = connection.query('select height from users where pin=1100');
return result;
}).then(function(rows){
console.log(JSON.parse(JSON.stringify(rows[0].height)));
connection.end();
return rows[0].height;
}).catch(function(error){
if (connection && connection.end) connection.end();
//logs out the error
console.log(error);
});
}
async function queryDb(){
try{
var height = await getResult();
console.log(height);
if(height){
console.log(height)
}
}catch(err){
console.log(err);
console.log('Could not process request due to an error');
return;
}
}
queryDb();
I expect the height to be returned in queryDb, however, the value is only shown in the getResult function and not returned to be used in the queryDb function.
I know the code may not be perfect as I'm new to node and I've been trying to find alternative ways to do this but
I've been trying to use async/await with MySQL in node but it returns an undefined value each time. Is there a reason why? Please find my code below.
const mysql = require('promise-mysql');
var connection;
const dbConfig = {
host: "hostname",
database: "dbname",
user: "username",
password: "passwords"
};
async function getResult(){
await mysql.createConnection(dbConfig).then(function(conn){
connection = conn;
var result = connection.query('select height from users where pin=1100');
return result;
}).then(function(rows){
console.log(JSON.parse(JSON.stringify(rows[0].height)));
connection.end();
return rows[0].height;
}).catch(function(error){
if (connection && connection.end) connection.end();
//logs out the error
console.log(error);
});
}
async function queryDb(){
try{
var height = await getResult();
console.log(height);
if(height){
console.log(height)
}
}catch(err){
console.log(err);
console.log('Could not process request due to an error');
return;
}
}
queryDb();
I expect the height to be returned in queryDb, however, the value is only shown in the getResult function and not returned to be used in the queryDb function.
I know the code may not be perfect as I'm new to node and I've been trying to find alternative ways to do this but
Share Improve this question asked Aug 23, 2018 at 12:03 JayJay 531 gold badge2 silver badges7 bronze badges 2-
1
Your
getResult
function doesn't return anything. – user5734311 Commented Aug 23, 2018 at 12:04 - @ChrisG Thank you, that was very silly on my part – Jay Commented Aug 23, 2018 at 12:09
1 Answer
Reset to default 7async function getResult(){
let connection;
try {
connection = await mysql.createConnection(dbConfig);
const result = await connection.query('select height from users where pin=1100');
console.log(result[0].height);
return result[0].height;
} finally {
if (connection && connection.end) connection.end();
}
}
Fixes the following problems:
- If you can use async/await, it's pointless to still use
then
for these situations.. - You don't need to JSON
stringify
andparse
if you're logging something. - If you catch an error to close a connection, you really should rethrow it so the function that calls
getResult
doesn't get garbage/undefined
back. Instead of rethrowing it, I just added afinally
block that always closes the connection, whether it was successful or not. - Since you're using async/await, your javascript engine should support
let
andconst
. It's better thanvar
=) - You weren't returning anything.
本文标签: javascriptNodejs Using asyncawait with mysqlStack Overflow
版权声明:本文标题:javascript - Node.js Using asyncawait with mysql - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741414050a2377405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论