如何用承诺Aync或等待
IVE开始学习NodeJS/ Express并在构建Web应用程序时,我决定使用MySQL2。我一直在遵循有关如何使所有工作正常工作的教程,并且一切似乎都很好,直到我发现承诺。为了缩短长话短说,我现在开始理解承诺的价值,但是,当我试图更改代码以容纳它们时,我没有得到相同的结果。
我以前的功能看起来像这样。
function checkIfLogedIn(req,res,next){
let membershipnumber = req.session.membershipno;
if(req.session.loggedin){
db.query('SELECT * FROM customers WHERE membershipnumber = ?', [membershipnumber], function(error, results, fields) {
if (error) {
response.send('Error connecting to the database '+error);
};
if (results.length > 0) {
req.user = {
name: '${results[0].first_name} ${results[0].last_name}',
membershipnumber: results[0].membershipnumber,
customer_id: results[0].id
}
}
next();
});
}
}
这很好。为了实施承诺,我首先更改了数据库文件
const mysql = require('mysql2');
//const mysql = require('mysql2/promise');
/////////////////////////////////////////////
//local SQL connection
/////////////////////////////////////////////
module.exports = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'letsgetkinky',//
database : 'nodelogin'
});
,然后在我的功能中进行了此操作,
const db = require('../../database')
async function checkIfLogedIn(req,res,next){
let membershipnumber = req.session.membershipno;
if(req.session.loggedin){
const data = await db.query('SELECT * FROM customers WHERE membershipnumber = 4');
console.log(data);
}
}
这导致下面的错误,
Error: You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface.
我不确定我在哪里出错,我尝试了文档,但是它不确定't似乎有效或保持一致。当我更改MySQL2/Prome时,请告诉我函数查询不存在。我真的很想了解我在哪里出错,以及我应该如何解释文档,就像有多种实现这一目标的方法一样。
Ive started learning nodejs/ express and in building a web app I decided to use mysql2. I've been following tutorials on how I get it all working and it all seemed to work fine until I discovered promises. To cut a long story short I've now started to understand the value of promises, however, when I tried to alter my code to accommodate them I did not get the same result.
my previous functions would look like this.
function checkIfLogedIn(req,res,next){
let membershipnumber = req.session.membershipno;
if(req.session.loggedin){
db.query('SELECT * FROM customers WHERE membershipnumber = ?', [membershipnumber], function(error, results, fields) {
if (error) {
response.send('Error connecting to the database '+error);
};
if (results.length > 0) {
req.user = {
name: '${results[0].first_name} ${results[0].last_name}',
membershipnumber: results[0].membershipnumber,
customer_id: results[0].id
}
}
next();
});
}
}
This worked fine. In the attempt to implement promises I first changed my database file
const mysql = require('mysql2');
//const mysql = require('mysql2/promise');
/////////////////////////////////////////////
//local SQL connection
/////////////////////////////////////////////
module.exports = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'letsgetkinky',//
database : 'nodelogin'
});
Then in my function, i have done this as a test
const db = require('../../database')
async function checkIfLogedIn(req,res,next){
let membershipnumber = req.session.membershipno;
if(req.session.loggedin){
const data = await db.query('SELECT * FROM customers WHERE membershipnumber = 4');
console.log(data);
}
}
This results in an error below
Error: You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise, which is a programming error. Try calling con.promise().query(), or require('mysql2/promise') instead of 'mysql2' for a promise-compatible version of the query interface.
Im not sure where im going wrong with this, I've tried following the documentation but it doesn't seem to work or be consistent. when i change mysql2/promise it then tells me that the function query does not exist. I would really like to understand where im going wrong on this and how i should be interpreting the documentation as it looks as though there are multiple ways of achieving this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了后代,我设法解决了自己的问题。我的数据库文件似乎正在引起问题。我尚未完全理解为什么,但是我还是设法在我的功能中使用来实现。而不是
createConnection
显然返回了我使用CreatePool
的承诺。这样,我就可以做const [fields,lows] =等待db.query('select *从subersionshipshiphumber = 4');
database.js file
For posterity, i managed to resolve my own issue. My database file seemed to be causing the issue. Im yet to fully understand why but nevertheless I have managed to achieve using
await
in my functions. Instead ofcreateConnection
which apparently returns a promise I usedcreatePool
. With this, I could then doconst [fields,rows] = await db.query('SELECT * FROM customers WHERE membershipnumber = 4');
database.js file