2 回答

TA貢獻1942條經驗 獲得超3個贊
實際上代碼需要像這樣更改:-
router.post('/uidcheck', async(req, res) => {
try {
const exists = await Register.findOne({ aadhaar : req.body.aadhaar})
if(exists == null){
res.statusCode = 200;
res.setHeader('Content-type', 'application/json');
res.json({success: true, status: 'You are allowed to register'});
}else{
res.statusCode=200;
res.setHeader('Content-type', 'application/json');
res.json({status: 'User already exists'});
}
}
catch(error){
res.status(500).send({message: "Error checking"});
}
})
我做了一些更正,它有效!

TA貢獻1853條經驗 獲得超18個贊
只有在有error. 錯誤就像網絡錯誤。
要求數據庫找到一個用戶并且它沒有找到一個用戶并不是一個錯誤,即你的代碼仍然會進入.then塊。您應該在此.then塊中檢查
.then( (user) => {
if (user) {
// all ok
} else {
// there is no user
}
}
并編寫您的業務邏輯。
添加回答
舉報