我有这样的代码块:
router.post('/users/login', async (req, res) => {
try {
const { email, password } = req.body
const user = await User.findByCredentials(email, password)
console.log(user) //false
if (!user) {
throw new Error('Login failed! Check authentication credentials')
}
const token = await user.generateAuthToken()
res.status(200).json({ user, token })
} catch (err) {
console.log(err) //Error: Login failed! Check authentication credentials at C:\Users...
res.status(400).json(err)
}
})
Run Code Online (Sandbox Code Playgroud)
一切正常,直到没有错误为止。当 Postman 中发生错误时,user我false只返回空对象{}。也用res.status(400).json({error: err})它给了我{ "err": {} }。
我想收到这样的对象{ "error": …