我想做一個簡單的登錄使用nodejs和sequelize,我已經有一個用戶在數據庫上,我嘗試發送信息作爲req.body,並檢查它是否存在於哪裏clausuresequelize找到登錄retrives總是成功
像這樣:
router.post('/', function (req, res, next) {
console.log("hi");
if (JSON.stringify(req.body) == "{}") {
return res.status(400).json({ Error: "Login request body is empty" });
}
if (!req.body.username || !req.body.password) {
return res.status(400).json({ Error: "Missing fields for login" });
}
User.find({ where: { username: req.body.username,password: req.body.password} })
.then(function (user) {
return res.status(200).json({ message: "loged in!" });
}).catch(function (err) {
return res.status(400).json({ Error: "There is no user with those fields" });
});
});
它總是進入的消息中loged,就算我送不數據庫上存在的數據,任何信息爲什麼它是怎麼回事?
你是絕對正確的:),我覺得別人是不是必要的,因爲你有return語句 –