I'm using put method in my routes but code not working

27 Views Asked by At

router.put('/edit-page/:id' , (req , res) => {

    console.log('Updating page:', req.params.id);
    console.log('Request body:', req.body.pageUrl);


pageModel.updateOne({ pageUrl : req.params.id } , {$set : {
              
        pageUrl : req.body.Page_Url , 
        pageHeading : req.body.Page_Heading  ,
        pageTitle : req.body.Page_Title  ,
        pageMetaDescription : req.body.Page_Meta_Description  ,
        pageMetaKeyword : req.body.Page_Meta_Keyword  ,
        // pagePhoto : req.file.filename ,
        pageDetails : req.body.Page_Details 
    
}})
.then((x) => {
    req.flash('success' , 'Updated');
     
    res.redirect('/admin/page/')
})

.catch((y) => {
    req.flash('danger' , 'Updated');
    res.redirect('/admin/page/')
})

})

?_method=PUT"> URL " class="form-control" placeholder="Page Url" required> Name " class="form-control" placeholder="Page Heading"> Title " placeholder="Page Meta Description"> Meta Description " placeholder="Page Meta Description"> Meta Keyword " placeholder="Page Meta Keyword"> Photo Page Details " placeholder="Page Details"> Submit

i am expecting the correct ans

1

There are 1 best solutions below

0
Cravecomm On

You must use express() instead of using express.Router() just like the following code:

const express = require("express");
const app = express();

app.use(express.json());

app.put("/edit-page/:id", async (req, res) => {
   // Handle the request here.
});