RoffyBC
RoffyBC

Reputation: 189

Post method is not sending JWT in nodeJS

I'm using post man to test my route and I dont know why it is not sending token to the database. Any suggestions?

//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)
    })
})

Upvotes: 0

Views: 75

Answers (1)

Zohaib Sohail
Zohaib Sohail

Reputation: 310

You are sending response twice that's why you are not gettin token in response from a request in postman. Try this code.

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

Upvotes: 1

Related Questions