Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda