所以,我有以下代碼:使用拒絕承諾處理不好HTTP調用?
function SignUp(req, res, next){
const userCreds = {
email: req.body.email,
password: req.body.password
}
//Username and password must exist
if(!userCreds.email || !userCreds.password){
res.status(422).send({ error: 'Email and Password required'});
throw new Error(('Email and Password Required'));
}
//See if email is already being used
Users.findOne({ email: userCreds.email })
.then(function(user){
//If user does exist, return Error
if(user){
res.status(422).send({ error: 'Email is in use'});
throw new Error(('Email and Password Required'));
}
//Else if email is true, create and save user error
const newUser = new Users(userCreds);
//Save the user
return newUser.save(); //Return promise
})
.then(function(doc){
//Respond saying all OK
res.json({
success: true,
email: doc.email
});
})
.catch(function(err){
if(err)
return next(err);
});
}
功能的上方通過一個快速的路線,這樣 app.get('/signup', SignUp);
在這段代碼中,有兩種不同的「錯誤」了可能出現我需要處理。一種錯誤的是用戶請求不能被處理(嘗試不供給電子郵件和密碼,或使用已經被使用的電子郵件創建一個帳戶)。第二種錯誤是我無法控制的錯誤:拒絕來自Mongoose包的承諾。
假設我收到了一個錯誤的請求,類型爲1的錯誤。我想通過將響應頭設置爲422來處理它,然後發送一條消息,說明爲什麼無法處理它。那時執行會結束。
如果我得到2類型的錯誤,我想打電話給next(error)
並在該點停止執行。
問題是,通過鏈接.then()
功能,我不能return
從一個代碼塊沒有跳轉到以下.next()
。要解決這個問題
一種方法是,當我得到1型或2的錯誤,並在.catch()
處理的情況下通過throw new Error()
拋出一個錯誤,但我不能確定多少,這會是好還是壞的實踐是。
我怎樣才能讓這個我可以在.then()
塊處理錯誤,然後停止執行?這是否是最好的方法呢?
有沒有更好的方式來處理這幾樣在高速的情況?我錯過了什麼?
謝謝!