【发布时间】:2020-10-10 21:27:09
【问题描述】:
以下代码给了我 jwt.sign 的错误(res.headers 不是函数) 有什么建议? 是因为语法吗?
app.post('/logIn', async (req, res) => {
//Checking if email exists
const user = await User.findOne({username:req.body.username})
if (!user) return res.status(400).send('Invalid Username')
//Password is correct
const validPass = await bcrypt.compare(req.body.password,user.password)
if (!validPass) return res.status(400).send('Invalid password')
//Create and assign a token
const token = jwt.sign({user},'secretkey')
res.headers('auth-token',token).send(token)
// res.send('Logged in ')
})
【问题讨论】:
-
错误是什么?提示您不想传递所有用户,否则将包括密码和其他无用信息,只需要一个 id
-
我收到未处理的承诺错误
-
在哪一行?如果你使用 async/await,你也应该使用 try/catch
-
我再次检查,它说 res.headers 不是函数
-
删除
s其res.header(..