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

292
Vistas
How can I update nested array in Firestore?

my database at the moment

Is there a way to update the likes and replies with firebase firestore?

I have reached that moment when I press on like on a single comment but I have only the reference to the document of the whole post , so I don't have a reference to the comments itself. I also reached the comment with the find method of the arrays but it's not a reference its the comment itself.

Thanks in advance, I am thinking I just have to reconsider how i structure my DB using firestore?

const addLikeToComment = async (
  postID: string,
  commentID: string,
  userID: string
): Promise<void> => {
  const currentPost = await getPostById(postID)
  const currentPostDocId = currentPost[0].docID!
  const currentPostRef = doc(db, 'posts', currentPostDocId)

  const commentToAddLikeTo = currentPost[0].comments.find(
    (currComment) => currComment.commentID === commentID
  )
  console.log(commentToAddLikeTo)
  if (!commentToAddLikeTo?.likes.includes(userID)) {
    await updateDoc(currentPostRef, {
        // add the like to comments - currentComment - likes array inside it push the userID
    })
  } else {
    await updateDoc(currentPostRef, {
        // remove the like
    })
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const addLikeToComment = async (
postID: string,
commentID: string,
userID: string
): Promise<void> => {
const currentPost = await getPostById(postID)
const currentPostDocId = currentPost[0].docID!
const currentPostRef = doc(db, 'posts', currentPostDocId)
const commentToAddLikeTo = currentPost[0].comments.find(
    (currComment) => currComment.commentID === commentID
)
const newLikes = commentToAddLikeTo?.likes

if (!commentToAddLikeTo?.likes.includes(userID)) {
    newLikes?.push(userID)
} else {
    const indexOfUserID = newLikes?.indexOf(userID)
    newLikes?.splice(indexOfUserID!, 1)
}

await updateDoc(currentPostRef, {
    ...currentPost[0],
    likes: newLikes,
})

}

This is how I solved this with some help from a co-worker. Basically I am manipulating the likes array directly and then updating the whole post information.

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