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

239
Views
Obtener resultado de eliminación múltiple por knex dentro de un bucle for

Estoy usando knexjs (constructor de consultas) en una aplicación CRUD. En una parte, es necesario eliminar de varias tablas, cada tabla varias filas e informar las filas eliminadas. El problema es que el método knex.del() devuelve una Promesa, y esa Promesa debe resolverse dentro de un bucle for. ¿Cómo puedo obtener el "informe" de las filas eliminadas en cada tabla en el objeto eliminado por ese bucle? Probé métodos como Promise.all , reduce( ) pero aún enfrentaba el mismo problema (objeto eliminado no resuelto).

 async function customDelete ( tableList) { //tableList is an object, where keys = tables, and values correspond to row ids to delete in each table const deleted = { }; for (const table of tableList) { if (table in aConditionCheckList) { console.log("table:", table, "-- ids to delete: ", tableList[table]); deleted[table] = knex(table).whereIn('id', tableList[table]).del() .returning('id'). .then( result => { deleted[table] = result; notDeleted = tableList[table].filter(x => !result.includes(x)); console.log("***deleted | notDeleted:", result, notDeleted); if(notDeleted.length){ console.warn('deleting', table, 'no such object(s)', notDeleted); } return result; }); } } return deleted; }

y la parte que llama customDelete () :

 if (req.body && req.body.sync && req.body.sync.delete) { sync.deleted = await customDelete (req.body.sync.delete); console.log("deleted rows:", sync.deleted) }

Sin await before knex(table).del( ) el objeto devuelto es Promise {<pending>} ,

 deleted[table] = knex(table).whereIn('id', tableList[table]).del().returning('id').then(...)

con await , el script se cuelga para siempre.

 deleted[table] = await knex(table).whereIn('id', tableList[table]).del().returning('id').then(...)

Resultado esperado: un objeto:

 deleted = { "table1": [1, 2, 4, 5, 12], "table2": [2,3,4,6]} //*etc.*

¿Cómo puedo solucionar esto?

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!