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

264
Views
How to get last element of document in firestore?
db.collection("todolist").get().then((querySnapshot) => {
console.log(querySnapshot[querySnapshot.length -1])})

This results in undefined. How do i get the last element of a document?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you're only interested in the last document, consider ordering the query, and requesting only one document, so save on the bandwidth and cost. Say you have a field named timestamp, you can get the latest one (and only that one) with:

db.collection("todolist")
  .orderBy("timestamp", "desc")
  .limit(1)
  .get().then((querySnapshot) => {
    console.log(querySnapshot.docs[0])
  })

Since you now only requested one document, you can use docs[0] to get at it.

about 4 years ago · Juan Pablo Isaza Report

0

querySnapshot isn't an array, so you can simply index into it. It's a QuerySnapshot object, and I strongly suggest you familiarize yourself with that API documentation. You can see that it has a docs property, which is an array.

querySnapshot.docs[querySnapshot.docs.length-1]

or

querySnapshot.docs[querySnapshot.size-1]
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!