Home > Enterprise >  How to delete item from mongodb on click of font awesome icon?
How to delete item from mongodb on click of font awesome icon?

Time:12-23

I am working on a inventory application and trying to delete item from mongodb when delete font awesome icon is clicked.

I looked at the MDN docs local library project enter image description here

Category_detail.pug

extends layout 
block head 
    link(rel="stylesheet", href="/stylesheets/category_detail.css")
    <script src="https://kit.fontawesome.com/abb39a57a2.js" crossorigin="anonymous"></script>
block content 
    h1 #{category.name} 
    table
        thead 
            tr 
                th Item name 
                th Price 
                th Update 
                th Delete
        tbody 
            each item in items
                tr 
                    td #{item.itemName}
                    td #{item.price}
                    td
                        //- Update iten details 
                        a(href="#") 
                            i()
                    td 
                        //- Delete item 
                        form(method="POST" action='')
                            div.form-group
                                input(type="hidden", name='itemid',required="true", value=item._id )
                            a()
                                i( type='submit')
                            
                        //- a(href="#")
                            //- i() 
            

Here is my github if you want to see any other file

CodePudding user response:

I don't what is a pug, but i knew HTML.. Also I am noob.. First thing, we can't click on tag..so, Instead we can add a click function on anchor tag and write our logic..(in your case, it's a delete logic)..

CodePudding user response:

I solved it. Here is the code for update icon

Pug file

td
  //- Update iten details 
      a( href=`/categories/${category._id}/update/${item._id}`  data-updateid=`${item._id}`) 
        i()

I simply added href to the anchor tag which contains the id of item that was clicked.After that in backend I used req.param.id to get the id of particular item clicked.

itemController

exports.itemUpdate_get = (req, res)=>{
    Items.findById(req.params.id)
    .populate('category')
    .exec(function(err, data){
        res.render('item_form', {itemData: data});
    })
}
  • Related