Reputation: 189
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
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