Skip to content
Permalink
3ff8db9eb5
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
77 lines (57 sloc) 1.58 KB
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