I’m constantly getting this error “Uncaught referenceerror: req not defined”
can somebody help me out, i’ve already tried other answers from stackoverflow but none seems working for me. please help me out, thanks in advance
here is my index.js
`const express = require("express");
const path = require("path");
const port = 8000;const
request = require('request');
const db = require('./config/mongoose');
const Contact = require('./models/contact');
const app = express();
app.set('view engine', 'ejs');
app.set('views',
path.join(__dirname,'views'));
app.use(express.urlencoded({extended:true}));
app.use(express.static('assets'))
var contactList = [
{name: 'Vijay chakradhari',
phone: '9222949595'},
{name: 'Vijay yaduvanshi',
phone: '9223949596'},
{name: 'Krishna chakradhari',
phone: '9224949595'}]
app.get('/', async function(req, res){
try {const contacts = await Contact.find({});
return res.render('home',{title: "Contact List",contact_list: contacts});}
catch (err) {console.log("error in fetching contacts from db");return;}
app.get('/practice', function(req, res){return res.render('practice',{'title': 'It is only Practice'})})});
app.post('/create-contact', async function(req, res){try {const newContact = await Contact.create({name: req.body.name,phone: req.body.phone});console.log(newContact);return res.redirect('back');} catch (err) {console.log('Error in creating a contact!');}});
app.get('/delete-contact', async function(req, res){
let id = req.query.id;
console.log('The contact id is:', req.query.id);
try {
await Contact.findByIdAndDelete(id);
res.redirect('back');
} catch (err) {
console.log('Error in deleting a contact in Database');
return;
}
});
app.listen(port, function(err){
if(err){console.log('Error in running the server', err)}
console.log('App is listening on port ' + port)
}); `
my home.ejs is as follows
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= title%></title>
<!--FONTAWESOME KIT-->
<script src="https://kit.fontawesome.com/9f41a01f34.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="/css/home.css">
</head>
<body>
<!-- <h1>Rendered with EJS</h1> -->
<h1 id="page-heading">
<i class="fa-duotone fa-address-card fa-xl" style="--fa-secondary-opacity: 0.5;"></i>
My Contact List</h1>
<div id="contact-list-container">
<ul>
<%
for (let i of contact_list){%>
<li class="contact">
<div class="details">
<p><%= i.name%>
<%= i.phone%>
</p>
</div>
<div class="delete-button">
<a href="/delete-contact/?id<%= i._id%>">
<i class="fas fa-window-close"></i>
</a>
</div>
</li>
<%}%>
</ul>
</div>
<form id="new-contact-form" action="/create-contact" method="post">
<input type="text" name="name" placeholder="enter your name" required>
<input type="number" name="phone" placeholder="enter your phone" required>
<button type="submit">Add Contact</button>
</form>
<script src="/JS/home.js"></script>
</body>
</html>
my mondodb database collection is defined as follows
const mongoose = require('mongoose');
const contactSchema = new mongoose.Schema({
name:{
type: String,
required: true
},
phone:{
type: String,
required: true
}
})
const Contact = mongoose.model('Contact', contactSchema);
module.exports = Contact;
and finally
Database configuration file
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/contacts_list_db');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'error connecting to database'));
db.once('open', function(){
console.log('Successfully connected to the DataBase');
});
I was using simple code to get the id of contact using
id = req.query.id;
but i didnot get it and instead i get this error "Uncaught referenceerror: req not found"