简体   繁体   English

events.js:183 抛出错误; // 未处理的“错误”事件 ^ nodeJS JWT

[英]events.js:183 throw er; // Unhandled 'error' event ^ nodeJS JWT

I'm new to the nodeJS and JWT.我是 nodeJS 和 JWT 的新手。 I want to create token on registration and confirm it on log in. But it keeps throwing syntax error and I dont know why.我想在注册时创建令牌并在登录时确认它。但它不断抛出语法错误,我不知道为什么。 Also please if I'm doing anything wrong besides syntax,tell me.如果除了语法之外我做错了什么,请告诉我。

  //Add new user A.K.A Registration

  app.post('/addUser', (req, res) => {
    const addUser = new User({username: req.body.username, password: req.body.password})
    addUser.save().then(result => res.status(200).json(result)).catch((err) => console.log(err))
    jwt.sign(addUser,'secretKey',{expiresIn:'30h'},(err,token)=>{
        res.json({token})
    })
})

//Log in A.K.A Sign Up
app.post('/logIn',verifyToken ,(req, res) => {
    User.findOne({username: req.body.username, password: req.body.password}).then(result => {
        if (result.username === req.body.username && result.password === req.body.password) {
            res.status(200).send({
                message: 'Successful login'})
            jwt.verify(req.token, 'secretkey', (err, authData) => {
                if(err) {
                    res.sendStatus(403);
                } else {
                    res.json({
                        message: 'Post created...',
                        authData
                    });
                }
            })
        } else {
            res.status(404).send({
                message: 'Invalid Login'
            })
        }
    })
})

you should pass an object to json method您应该将 object 传递给 json 方法


// addUser.save().then(result => res.status(200).json(result))

.then(result => {

res.json({result})

})


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM