我在承诺链中有 catch() 但仍然收到未处理的承诺拒绝
I have catch() in promise chain but still gets unhandled promise rejection
我发现很多线程都有同样的问题,但几乎所有线程都没有 catch() 或错误地使用了 promise 链。
我正在测试用户输入错误的密码或身份并且登录失败的情况。
在我的代码中,我认为我使用 catch() 正确地使用了承诺链,但我仍然遇到未处理的承诺拒绝错误。
该站点应显示类似于网站上的错误消息“密码错误”。
代码写在node.js和express.js
这是我的代码:
app.js
app.post('/:login',(req,res)=>{
const userId = req.body.userId;
const userPw = req.body.userPW;
try{
if(userId.length<5&&userPw.length<5){
throw "Id and password must be minimum 5 characters";
}else if(userId.length>=5&&userPw.length<5){
throw "Password must be minimum 5 characters";
}else if(userId.length<5&&userPw.length>=5){
throw "Id must be minimum 5 characters";
}else{
dbFile.checkIfUserExists(userId,userPw,res)
.then((response)=>{
console.log(response[0].userId);
return response[0].userId;
}).catch((errMessage)=>{
console.log(errMessage);
throw errMessage;
})
}
}
catch(e){
console.log(e);
res.send({
errorMessage:e
})
}
});
userListDB.js
const mysql = require('mysql');
function createMySQLConnection(){
const connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'',
database:'chatdatabase'
});
return connection;
}
function connectToDB(connection){
try{
connection.connect(function(err){
if(err){
throw "Sorry, something happened. Please try later";
}
})
}catch(error){
return error;
}
}
module.exports={
checkIfUserExists:function(id,pw,res){
const connection = createMySQLConnection();
if(connectToDB(connection)===undefined||connectToDB(connection)===null){
const sql = "SELECT * FROM userlist WHERE userId=? AND password=?";
return new Promise((resolve,reject)=>{
connection.query(sql,[id,pw],(error,result)=>{
try{
if(error){
throw error;
}
else if(result.length===0){
throw "It seems like your id and password don't match. please try again with different id and password";
}
else{
resolve(result);
}
}catch(e){
reject(e);
}
})
})
}
}
}
我知道我没有进行密码加密,但我会在解决此问题后进行加密。
正如您在代码中看到的,当用户发送 POST 登录请求时,app.js 中的 app.post 将验证数据,如果没有问题,它将从 [=] 调用 dbFile.checkIfUserExists 34=]。 dbUserList returns 一个承诺所以我在 app.post.
中用 .then().catch() 做了一个承诺链
但我还是得到了
It seems like your id and password don't match. please try again with different id and password
(node:7896) UnhandledPromiseRejectionWarning: It seems like your id and password don't match. please try again with different id and password
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7896) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7896) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我发现 console.log 在 then().catch() 中工作,所以在两个 try catch 中应该没有任何问题,我不明白为什么我的代码仍然显示未处理的承诺拒绝
确实,您有一个未处理的承诺拒绝:
.catch((errMessage)=>{
console.log(errMessage);
throw errMessage;
})
.catch
位于承诺链的末尾,因此 returns 是一个承诺。现在,如果您进入 catch
回调和 throw
,那么这将使该承诺处于拒绝状态,并且没有更多的处理程序来处理该拒绝。
你应该直接在那个地方发送而不是扔:
res.send({errorMessage: errMessage})
我发现很多线程都有同样的问题,但几乎所有线程都没有 catch() 或错误地使用了 promise 链。 我正在测试用户输入错误的密码或身份并且登录失败的情况。 在我的代码中,我认为我使用 catch() 正确地使用了承诺链,但我仍然遇到未处理的承诺拒绝错误。 该站点应显示类似于网站上的错误消息“密码错误”。
代码写在node.js和express.js
这是我的代码: app.js
app.post('/:login',(req,res)=>{
const userId = req.body.userId;
const userPw = req.body.userPW;
try{
if(userId.length<5&&userPw.length<5){
throw "Id and password must be minimum 5 characters";
}else if(userId.length>=5&&userPw.length<5){
throw "Password must be minimum 5 characters";
}else if(userId.length<5&&userPw.length>=5){
throw "Id must be minimum 5 characters";
}else{
dbFile.checkIfUserExists(userId,userPw,res)
.then((response)=>{
console.log(response[0].userId);
return response[0].userId;
}).catch((errMessage)=>{
console.log(errMessage);
throw errMessage;
})
}
}
catch(e){
console.log(e);
res.send({
errorMessage:e
})
}
});
userListDB.js
const mysql = require('mysql');
function createMySQLConnection(){
const connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'',
database:'chatdatabase'
});
return connection;
}
function connectToDB(connection){
try{
connection.connect(function(err){
if(err){
throw "Sorry, something happened. Please try later";
}
})
}catch(error){
return error;
}
}
module.exports={
checkIfUserExists:function(id,pw,res){
const connection = createMySQLConnection();
if(connectToDB(connection)===undefined||connectToDB(connection)===null){
const sql = "SELECT * FROM userlist WHERE userId=? AND password=?";
return new Promise((resolve,reject)=>{
connection.query(sql,[id,pw],(error,result)=>{
try{
if(error){
throw error;
}
else if(result.length===0){
throw "It seems like your id and password don't match. please try again with different id and password";
}
else{
resolve(result);
}
}catch(e){
reject(e);
}
})
})
}
}
}
我知道我没有进行密码加密,但我会在解决此问题后进行加密。 正如您在代码中看到的,当用户发送 POST 登录请求时,app.js 中的 app.post 将验证数据,如果没有问题,它将从 [=] 调用 dbFile.checkIfUserExists 34=]。 dbUserList returns 一个承诺所以我在 app.post.
中用 .then().catch() 做了一个承诺链但我还是得到了
It seems like your id and password don't match. please try again with different id and password
(node:7896) UnhandledPromiseRejectionWarning: It seems like your id and password don't match. please try again with different id and password
(Use `node --trace-warnings ...` to show where the warning was created)
(node:7896) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7896) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我发现 console.log 在 then().catch() 中工作,所以在两个 try catch 中应该没有任何问题,我不明白为什么我的代码仍然显示未处理的承诺拒绝
确实,您有一个未处理的承诺拒绝:
.catch((errMessage)=>{
console.log(errMessage);
throw errMessage;
})
.catch
位于承诺链的末尾,因此 returns 是一个承诺。现在,如果您进入 catch
回调和 throw
,那么这将使该承诺处于拒绝状态,并且没有更多的处理程序来处理该拒绝。
你应该直接在那个地方发送而不是扔:
res.send({errorMessage: errMessage})