Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sports_Club/authentication.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
77 lines (57 sloc)
1.58 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const router = express.Router() | |
const User = require('../models/user') | |
router.get('/:id',getUsers,(req,res) => { | |
console.log(res.data) | |
res.send(res.data); | |
}) | |
router.post('/',async(req,res)=>{ | |
console.log(req.body.name); | |
const user=new User({ | |
email:req.body.email, | |
student_id:req.body.student_id, | |
name:req.body.name, | |
password:req.body.password, | |
type:req.body.type | |
}) | |
try{ | |
const newUser=await user.save() | |
res.status(201).json(newUser) | |
} | |
catch(err){ | |
res.status(201).json({message:err.message}) | |
} | |
}) | |
async function getUsers(req,res,next){ | |
let data; | |
try{ | |
data=await User.findById(req.params.id); | |
if(data==null) | |
{ | |
return res.status(404); | |
} | |
} | |
catch(err){ | |
return res.status(500); | |
} | |
res.data=data; | |
next(); | |
} | |
router.get('/:id', getStudent, (req,res) => { | |
console.log(res.studentdetails.email) | |
res.send(res.studentdetails.name) | |
}) | |
async function getStudent(req,res,next) { | |
let studentdetails | |
try { | |
studentdetails = await Club.findById(req.params.id) | |
if(studentdetails == null){ | |
return res.status(404).json({message: "cannot find club"}) | |
} | |
} catch (err){ | |
return res.status(500).json({message: err.message}) | |
} | |
res.studentdetails = studentdetails | |
next() | |
} | |
module.exports = router |