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

111
Views
Firebase Database V9 Devolver el último elemento

Tengo un sistema de chat en el que un chat contiene varios mensajes. La estructura se ve así a continuación:

 - messages - ChatId123 - MesageId123 - MessageId456

Solo quiero recuperar el último mensaje enviado en este caso MessageId456 Supongo que necesito usar orderByKey() . Las claves se generan usando push() ¿cómo puedo consultar y filtrar correctamente el último elemento?

 const query = query(ref(db, `messages/ChatId123`), orderByKey()) return query
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

la forma más fácil sería agregar un campo de marca de tiempo en el objeto del mensaje como "SendAt" y ordenar por este valor:

 const q = query(ref(db, `messages/ChatId123`), orderBy("SendAt", "desc"), limit(1));

ejemplo del lado de Firestore

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar limitToLast para obtener solo los últimos elementos de una consulta:

 query(ref(db, `messages/ChatId123`), orderByKey(), limitToLast(1))

Para obtener más información sobre esto, consulte la documentación de Firebase sobre el filtrado de datos .

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!