Home > Software engineering >  edit a product from admin panel of eCommerce website project using nodejs with express.js, hbs and m
edit a product from admin panel of eCommerce website project using nodejs with express.js, hbs and m

Time:05-28

edit button

                    <td>
                        <a href="/admin/edit-product/{{this._id}}" >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

section>
<div >
    <div >
        <div >
            <h2 >Edit Products</h2>
            <form action="/admin/edit-product/:{{product._id}}" method="post" enctype="multipart/form-data">
                <label for="">Number</label>
                <input name="no"  type="text" value="{{product.no}}">

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

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

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

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

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

                <button type="submit" >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.

CodePudding user response:

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

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