最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

'Uncaught ReferenceError: req is not defined' 当我使用 req.query.id 时在控制台中出现此错误

网站源码admin56浏览0评论

'Uncaught ReferenceError: req is not defined' 当我使用 req.query.id 时在控制台中出现此错误

'Uncaught ReferenceError: req is not defined' 当我使用 req.query.id 时在控制台中出现此错误

'Uncaught ReferenceError: req is not defined' 在控制台中出现这个错误 当我使用 req.query.id

这是我的 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)   
    });

home.ejs

    <!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=".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>

mongoose.js 文件

    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');
    }); 
Database collection
    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;

我希望在使用“删除联系人按钮”时获得联系人 ID,但我却收到此错误“未定义(请求)”

'Uncaught ReferenceError: req is not defined' 当我使用 req.query.id 时在控制台中出现此错误

回答如下:

您可以在放置

console.log(req.query)
之前
console.log(req.query.id)
检查您是否从请求中获取ID。 如果您没有从
req.query
获得 id,请尝试以下

<div class="delete-button">
    <a href="/delete-contact?id<%=i._id%>">
      <i class="fas fa-window-close"></i>
    </a>
</div>
发布评论

评论列表(0)

  1. 暂无评论