[英]Why is this endpoint in my API returning [] and not 404 error?
I'm currently creating some API with Zeit Now, and I was trying to implement 404 error when the user
variable is []
(an empty array), but when I fetch this endpoint, API is sending []
aswell.我目前正在使用 Zeit Now 创建一些 API,当user
变量为[]
(空数组)时,我试图实现 404 错误,但是当我获取此端点时,API 也在发送[]
。 How can I implement it?我该如何实施? Is it because the user
is a promise?是因为user
是承诺吗?
const db = require('../../lib/db')
const escape = require('sql-template-strings')
module.exports = async (req, res) => {
const user = await db.query(escape`
SELECT *
FROM users
WHERE username = ${req.body.username}
AND password = ${req.body.password}
`)
if (user === []) {
res.status(404).json({ message: 'User with these credentials doesn\'t exist.' })
return false
}
res.status(200).json(user)
}
Because因为
[] === [] // false
So you want to check the length
property所以你想检查length
属性
if (user.length === 0) {
res.status(404).json({ message: 'User with these credentials doesn\'t exist.' })
return false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.