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

120
Views
Use una variable fuera del objeto para que sea la variable del objeto

tengo este codigo

 for (var i = 0; i < lootbox.length; i++) { const { type } = lootbox[i] const query = type + "_lootbox.hunt"; await lb.findOneAndUpdate( { userID: message.author.id }, { $inc: { query: 1 } } ); }

el código lee la query como su propia variable en lugar del type + "_lootbox.hunt" , ¿hay alguna forma de usar esa consulta dentro de $inc ? porque quiero automatizarlo usando el ciclo

también conocido como clave de objeto dinámico

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El problema aquí es que está tratando de crear un objeto sobre la marcha, pero no puede establecer una clave como esa dinámicamente.

Para lograrlo, debe preparar este objeto antes de usarlo.

Y, en tal caso, su código sería algo como esto,

 for (var i = 0; i < lootbox.length; i++) { const { type } = lootbox[i] const query = type + "_lootbox.hunt"; const tempObject = {}; // Creating an empty Object tempObject[query] = 1; // Dynamic key usage await lb.findOneAndUpdate( { userID: message.author.id }, { $inc: tempObject } // Using the tempObject ); }
about 4 years ago · Juan Pablo Isaza Report

0

Según tengo entendido, lo que quiere es esto: en cada iteración, la query se convierte en type + "_lootbox.hunt" , por ejemplo, page1_lootbox.hunt . Y lo que quieres obtener es { $inc: { "page1_lootbox.hunt": 1 }} . Si es así, una solución simple será

 { $inc: { [query]: 1 } } // { $inc: { "page1_lootbox.hunt": 1 } }

Avísame si quieres algo diferente.

about 4 years ago · Juan Pablo Isaza Report
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!