Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda