Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

edit a product from admin panel of eCommerce website project using nodejs with express.js, hbs and mongodb

edit button

                    <td>
                        <a href="/admin/edit-product/{{this._id}}" class="btn btn-primary">edit</a>
                    </td>

router.get of edit product link that i given to the edit button

var ProductHelpers = require('../helpers/product-helpers')
router.get('/edit-product/:id',async (req,res)=>{
  let product=await ProductHelpers.getProductDetails(req.params.id)
  console.log(product)
  res.render('admin/edit-products',{product})
}) {{!--here i have rendered edit products page and passed product variable --}}

edit-products.hbs page

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

section>
<div class="container">
    <div class="row mt-4">
        <div class="col-md-6">
            <h2 class="text-center">Edit Products</h2>
            <form action="/admin/edit-product/:{{product._id}}" method="post" enctype="multipart/form-data">
                <label for="">Number</label>
                <input name="no" class="form-control" type="text" value="{{product.no}}">

                <label for="">Name</label>
                <input name="name" class="form-control" type="text" value="{{product.name}}">

                <label for="">Catagery</label>
                <input name="catagery" class="form-control" type="text" value="{{product.catagery}}">

                <label for="">Price</label>
                <input name="price" class="form-control" type="text" value="{{product.price}}">

                <label for="">Description</label>
                <input name="description" class="form-control" type="text" value="{{product.description}}">

                {{!-- <label for="">Image</label>
                <input name="image" class="form-control" type="file"> --}}

                <button type="submit" class="btn btn-primary mt-4">Update</button>
            </form>
        </div>
    </div>
</div>

**router.post of the link edit-product that i gave inside the form action in the edit-products.hbs page**

var ProductHelpers = require('../helpers/product-helpers')
router.post('/edit-product/:id',(req,res) => {
  ProductHelpers.updateProducts(req.params.id,req.body).then(()=>{
    res.redirect('/admin/')
  })
})

product-helpers.js page

getProductDetails:(Proid)=>{
    return new Promise((resolve,reject)=> {
        db.get().collection(collection.PRODUCT_COLLECTION).findOne({_id:objectId(Proid)}).then((product)=>{
            resolve(product)
        })
    })
},
updateProducts:(Proid,ProDetails)=>{
    return new Promise ((resolve,reject)=>{
        db.get().collection(collection.PRODUCT_COLLECTION).updateOne({_id:objectId(Proid)},{
            $set:{
                no:ProDetails.no,
                name:ProDetails.name,
                catagery:ProDetails.catagery,
                catagery:ProDetails.price,
                description:ProDetails.description,
            }
        }).then((response)=>{
            resolve()
        })
    })
}

browser
edit-button in browser
edit button

when i click edit button it takes me to this page
edit product page

all okey!.. but the problem is next
I changed the number and clicked update
the result is
result in browser

at the same time the result in console is:-
console result

I hope I have correctly explained and u have got it everything, please help me if you know to solve, if you have any doubt in my question just ask me.

>Solution :

Watch out for the : in the <form> action, change it to:

<form action="/admin/edit-product/{{product._id}}" method="post" enctype="multipart/form-data">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading