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
Am new to firebase and cant seem to order my data i have tried using query and orderBy()

i have tried using query and orderBy() and firstly cant even properly fit it into my code

methods:

async saveMessage(){
  try {
    const docRef = await addDoc(collection(db, "chat"), {
      message:this.message,
      createdAt: new Date()
    });
    console.log("Document written with ID: ", docRef.id);
  } catch (e) {
    console.error("Error adding document: ", e);
  }
  this.message= null;
},

async fetchMessages(){

    const querySnapshot = await getDocs(collection(db,"chat"));
     let allMessages = [];
    querySnapshot.forEach((doc) =>{
      allMessages.push(doc.data());
    })
    this.messages = allMessages;
  }
},
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To order results, you add a query with an ordering clause:

import { query, orderBy, ... } from "firebase/firestore";  

const querySnapshot = await getDocs(query(collection(db,"chat"), orderBy("name")));

I recommend reading the Firebase documentation on ordering and limiting data, from where I got the changes I made to your code.

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!