Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
¿Cuál es la forma correcta de eliminar un elemento usando Express/MongoDB?

Soy un poco nuevo en el uso de NodeJS/Express/MongoDB y tengo un problema al intentar eliminar un elemento con el método deleteOne integrado. Al hacer clic en el ícono de la papelera (fa fa-trash) en mi archivo ejs, espero que el elemento se elimine, pero en realidad no está eliminando nada.

Aquí tengo mi código del lado del servidor usando el método de eliminación:

 app.delete('/deleteBill', (request, response) => { db.collection('bills').deleteOne({billName: request.body.billNameS}) .then(result => { console.log(`bill Deleted`) response.json('bill Deleted') }) .catch(error => console.error(error)) })

Aquí está mi código del lado del cliente que básicamente realiza una búsqueda en la ruta del lado del servidor:

 const deleteText = document.querySelectorAll('.fa-trash') Array.from(deleteText).forEach((element)=>{ element.addEventListener('click', deleteBill) }) async function deleteBill(){ const bName = this.parentNode.childNodes[1].innerText try{ const response = await fetch('deleteBill', { method: 'delete', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ 'billNameS': bName }) }) const data = await response.json()//this response came from the server location.reload() console.log(data) console.log(`bName is: ${bName} .`) }catch(err){ console.log(err) } }

y aquí está mi código ejs:

 <!-- Looping through bills --> <% for (let i = 0; i < info.length; i++) {%> <% const billName = info[i].billName const capitalizedBillName = billName.charAt(0).toUpperCase() + billName.slice(1) %> <section class="billBox billCard w-50"> <div> <h2>Name of Bill</h2> <h3><%= capitalizedBillName %></h3> </div> <div> <h2>Amount Due</h2> <h3><%= `$` + info[i].billCost %></h3> </div> <div> <h2>Due Date</h2> <h3><%= info[i].billDue %></h3> </div> <div class="modifyBill"> <span class='fa fa-trash'> </div> </section> <% } %>

Creo que tiene algo que ver con la forma en que trato de manejar las cosas en el javascript del lado del cliente, pero no puedo identificar exactamente lo que estoy haciendo mal. Sería bueno tener un par de ojos diferentes para ver esto. Siento que la forma en que intento eliminar el elemento no es la mejor práctica estándar.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!