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

198
Views
agregar objetos a la matriz sin sobrescribir

tengo mi codigo actual

 let array = {}; array[date] = [ { Name: Name, Cell: Cell, Age: Age, userId: userId, Time: time, Type: type, read: false, }, ];

que sube a firebase así ingrese la descripción de la imagen aquí

pero cada vez que agrego una nueva entrada para la misma fecha, sobrescribe los datos existentes. Quiero que los datos se agreguen al mapa "1", etc. en lugar de sobrescribir los datos en el mapa "0".

actualización: he intentado lo siguiente y estoy recibiendo un error. No estoy seguro si hice esto correctamente, todavía estoy aprendiendo.

 let array = {}; array[date] = []; try { await firebase .firestore() .collection("Notifications") .doc(`${helper}`) .update( { array: firebase.firestore.FieldValue.arrayUnion({ Name: Name, Cell: Cell, Age: Age, userId: userId, Time: time, Type: type, read: false, }), }, { merge: true } ); } catch (e) { alert(e); console.log(e); }

actualizar con push:

 // at beginning of script let array = {}; // later, when you want to add to it if (!array[date]) { array[date] = []; } array[date].push({ Name: Name, Cell: Cell, Age: Age, userId: userId, Time: time, Type: type, read: false, }); try { await firebase .firestore() .collection("Notifications") .doc(`${helper}`) .set( array, { merge: true } ); } catch (e) { alert(e); console.log(e); } },

Solución:

 try { await firebase .firestore() .collection("Notifications") .doc(`${helper}`) .update({ [date]: firebase.firestore.FieldValue.arrayUnion({ Name: Name, Cell: Cell, Age: Age, userId: userId, Time: time, Type: type, read: false, }), }); } catch (e) { alert(e); console.log(e); }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Dado que configuró todo el campo 2022-01-03 , está sobrescribiendo cualquier valor existente en ese campo.

Si desea fusionar el nuevo valor/objeto que especifica con los valores existentes en un campo, use el operador array-union .

about 4 years ago · Juan Pablo Isaza Report

0

Use push() para agregar a una matriz.

 // at beginning of script let array = {}; // later, when you want to add to it if (!array[date]) { array[date] = []; } array[date].push({ Name: Name, Cell: Cell, Age: Age, userId: userId, Time: time, Type: type, read: false, });
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!