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

143
Vistas
Sorting Firebase data

I'm making a simple todo app with ReactJS and Firebase and I want to sort my todos by the time they were made. I get that I need to use server timestamps, but can't figure out a way to implement sorting to my way of displaying Firebase data. This is how I fetch the data:

useEffect(() => {
      const getTodos = async() => {
        const data = await getDocs(todosRef);
        setNewTodos(data.docs.map((doc) => ({...doc.data(), id: doc.id})))
      }
      getTodos();
    }, [])

And this how I add new todos to the database:

await addDoc(todosRef, {
      text: input,
      dueDate: date,
      comments: [],
      //timestamp: firebase.firestore.FieldValue.serverTimestamp()
    });
    setInput('');
    window.location.reload();
  }

Plus, when I try to add the new todo with the timestamp nothing happends. It won't push it to the database, so that's why it's commented out.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

For serverTimestamp you can just directly import it in modular v9.

import { addDoc, serverTimestamp } from "firebase/firestore";

await addDoc(todosRef, {
    timestamp: serverTimestamp()
 })

As for timestamp sorting you can simply generate a query and order by timestamp descending.

const q = query(todosRef, orderBy("timestamp", "desc");
const data = await getDocs(q);
about 4 years ago · Santiago Gelvez 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