Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple orderBy() in Firestore Query Produces Error

I want to query Firestore db and order by two fields. Official docs suggest I can simply combine the orderBy statements via orderBy('field1').orderBy('field2') but when I run similar code, I get the following error in console:

Project.js:62 Uncaught (in promise) TypeError: (0 , firebase_firestore__WEBPACK_IMPORTED_MODULE_0__.orderBy)(...).orderBy is not a function

My code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const projectRef = doc(db, 'projects', projectId)
const docSnap = await getDoc(projectRef)

if(docSnap.exists()) {
    const imagesInfoRef = collection(db, 'projectGalleryImages')
    const imagesInfoDocsSnapshot = await getDocs(query(imagesInfoRef, where('project', '==', projectRef), orderBy('displayOrder').orderBy('createdOn')))
    imagesInfoDocsSnapshot.docs.map(imageInfo => {
        const imageRef = ref(storage, `project/${projectId}/images/${imageInfo.fileName}`)
        loadImageURL(imageRef)
    })
}

Why am I getting this error?

>Solution :

You’re mixing the older namespaced syntax with the newer modular syntax. In the latter, each orderBy call is a standalone instruction:

query(imagesInfoRef, 
  where('project', '==', projectRef),
  orderBy('displayOrder'), // 👈 comma here, instead of a dot
  orderBy('createdOn')
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading