代码之家  ›  专栏  ›  技术社区  ›  Teja

更新用户详细信息时无法POST/update/“”

  •  0
  • Teja  · 技术社区  · 3 年前

    “无法发布/更新/”我在更新用户详细信息时遇到错误

    从注册到登录,显示详细信息并删除它,一切都很顺利,但当我尝试更新时,它会说“无法发布/更新/”

    节点js

    const express = require('express')
    const bodyParser = require('body-parser')
    const path = require('path')
    const mongoose = require('mongoose')
    
    const app = express()
    
    app.use(express.static(path.join(__dirname, 'public')))
    
    app.use(bodyParser.urlencoded({
        extended: true
    }))
    
    mongoose.connect('mongodb://localhost:27017/mydb', {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    
    const userSchema = new mongoose.Schema({
        name : String,
        mobile: Number,
        email: String,
        password: String
    })
    
    const User = mongoose.model('user', userSchema)
    
    //to store data into database...
    app.post('/signup', (req, res) => {
        var name = req.body.Fname
        var email = req.body.email
        var mobile = req.body.mobile
        var password = req.body.password
    
        var data = new User({
            'name' : name,
            'email': email,
            'mobile': mobile,
            'password': password
        })
    
        data.save()
            .then(() => {
                console.log('user saved to database')
                res.redirect('/signIn.html')
            })
            .catch((err) => {
                console.log('Error')
            })
    })
    
    //to display starting page (signup page)
    app.get('/', (req, res) => {
        res.sendFile(path.join(__dirname, 'index.html'))
    })
    
    app.set('view engine', 'ejs')
    
    //to display the user details...
    app.post('/login', async(req, res) => {
       try {
            const email = req.body.email
            const password = req.body.password
    
            const userDetails = await User.findOne({email: email})
            if(userDetails.password === password) {
                res.render('user', {user: userDetails})
            }
            else {
                res.send('Invalid Password')
            }
       }
       catch(err) {
            res.send('Invalid UserID')
       }
    })
    
    //for deleting the user...
    app.post('/delete/:id', async(req, res) => {
        try {
            const id = req.params.id
            await User.deleteOne({_id : id})
            res.redirect('/');
        }
        catch(err) {
            res.send('Error while Deleting')
        }
    })
    
    //for editing the user details...
    app.get('/edit/:id', (req, res) => {
        try {
            const id = req.params.id
            const user = User.findOne(id)
            
            res.render('update', {user: user})
        }
        catch(err) {
            res.send('Error while editing')
        }
    })
    
    //Facing an error while calling the update "cannot post/update/"
    
    app.post('/update/:id', async(req, res) => {
        try {
            const id = req.body.id
    
            const newData = {
                name : req.body.Fname,
                email : req.body.email,
                mobile : req.body.mobile,
                password : req.body.password
            }
    
            await User.findOneAndUpdate({id : id}, newData)
            
            res.redirect('/signIn.html')
        }
        catch(err) {
            res.send('Error while updating')
        }
    })
    
    app.listen(8000, () => {
        console.log('server is running');
    })
    
    console.log("Everything runs perfect")
    

    用于注册的Html

    <form action="/Signup" id="container" method="post">
        <h1>Sign Up</h1>
        <input type="text" placeholder="Enter Name" name="Fname" required>
        <input type="number" placeholder="Enter Mobile" name="mobile" required>
        <input type="email" placeholder="Enter Email" name="email" required>
        <input type="Password" placeholder="Enter Password" name="password" required>
        <input type="password" placeholder="Confirm Password" name="Cpassword" required>
        <input type="submit" value="sign up" class="btn">
    </form>
    

    用于登录的Html

    <form action="/login" id="container" method="post">
        <h1>Sign IN</h1>
        <input type="email" placeholder="Enter Email" name="email">
        <input type="Password" placeholder="Enter Password" name="password">
        <input type="submit" value="sign up" class="btn">
    </form>
    

    用于显示用户详细信息的EJS

    <div class="container">
        <h1>User Details</h1>
        <div>
            <h2>Username: <%= user.name %></h2>
            <h3>Email: <%= user.email %></h3>
            <h3>Mobile: <%= user.mobile %></h3>
        </div>
        <div class="btns">
            <form action="/edit/<%= user._id%>" method="get">
                <input type="submit" value="Edit">
            </form>
            <form action="/delete/<%= user._id%>" method="post">
                <input type="submit" value="Delete" name="deleteBtn">
            </form>
        </div>
    </div>
    

    用于更新用户详细信息的EJS

    <form action="/update/<%= user._id%>" id="container" method="post">
        <h1>Update</h1>
        <input type="text" placeholder="Enter Name" name="Fname">
        <input type="number" placeholder="Enter Mobile" name="mobile">
        <input type="email" placeholder="Enter Email" name="email">
        <input type="password" placeholder="Enter Password" name="password">
        <input type="submit" class="btn">Update</input>
    </form>
    
    0 回复  |  直到 3 年前
    推荐文章