I'm new to Express JS and MongoDB so please go easy on me. I'm following a tutorial of web development by colt steele. here is my code
app.get("/:id",async(req,res)=>
{
const id= req.params['id'];
console.log(id);
const students= await Student.findById(id);
console.log(students);
res.render("studentdata/show",{students});
})
I'm trying to get the id using req.params
but it is returning favicon.ico
.
This is the error I got
error screenshot
I tried using params['name']
(name field refers to student name in DB) which worked without any error. I also tried using _id
which gave me another error. I'm expecting params['id'] to return the id of the entry.
CodePudding user response:
I am Assuming that you are developing your application in localhost. Its always a best practice to keep your backend stuffs seperate. Instead of giving the route path as app.get('/:id') you could have used
app.get('/api/:id',async(req,res) =>{
const id= req.params['id'];
console.log(id);
const students= await Student.findById(id);
console.log(students);
res.render("studentdata/show",{students});
})
And the url for this should be like this :
http://localhost:port_number/api/student_id Here port number should be replaced with the port number on which your server is running . For example - 5000 And student_id should be replaced with student id For example - 12345 And the url shoud be like following: http://localhost:5000/api/12345