我试图登录,但得到400坏的请求和“错误的凭据”。
app.post("/signin", (req, res) => {
const {email, password} = req.body;
db.from ('login').select('*').eq("email", email)
.then(data => {
const isValid = bcrypt.compareSync(password, data[0].hash)
if (isValid) {
return db.from ('users').select('*')
.eq ("email", email)
.then(resp => res.json(resp[0]))
.catch(err => res.status(400).json("failed to get user"))
} else {
res.status(400).json("wrong credentials")
}
})
.catch(err=>res.status(400).json("wrong credentialsss")) // <-- HERE
})
然而,当我更新代码如下,我可以成功地获取数据。我猜问题是中间的一部分,但不知道。
app.post("/signin", (req, res) => {
const {email, password} = req.body;
db.from ('login').select('*').eq("email", email)
.then(data => res.json(data))
.catch(err=>res.status(400).json("wrong credentialsss"))
})
编辑:这是我从第二个代码中得到的
{
"error": null,
"data": [
{
"id": 1,
"hash": "$2a$10$JJ/i0c1bcu/1ZcV8f8DSGOipZUm5FLTGmvEygPjdpny3k0DI9/jrC",
"email": "admin@gmail.com"
}
],
"count": null,
"status": 200,
"statusText": "OK"
尝试记录错误
"message": "Cannot read properties of undefined (reading 'hash')",
"error": {}
2条答案
按热度按时间gupuwyp21#
尝试将“数据[0].hash”替换为“数据.数据[0].hash”
您在中接收到一个“data”对象,然后需要访问包含对象数组的“data”属性
qq24tv8q2#
很抱歉,我想发表评论,但我的声誉使我无法发表评论。因此,在此提出此问题。您是否尝试打印data [0]或data,而不是打印data [0].hash,以确保您以正确的格式获取它,并且不需要转换。