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

407
Vistas
Why error Expected first argument to collection() to be a CollectionReference

I am getting the following error when trying to get all documents from a collection in Firestore using firebase admin SDK:

[nuxt] [request error] Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

I am testing via a simple console.log:

/server/api/posts.js:

import { firestore } from '../utils/firebase'
import { collection, getDocs } from 'firebase/firestore'

export default defineEventHandler(async (event) => {
  const colRef = collection(firestore, 'posts')
  console.log(colRef)
})

Here is how I am initializing firestore:

/server/utils/firebase.js:

import { initializeApp, cert } from 'firebase-admin/app'
import { getFirestore } from 'firebase-admin/firestore'
import serviceAccount from '../../service-account.json'

export const app = initializeApp({
  credential: cert(serviceAccount)
})

export const firestore = getFirestore()

Note: The following code works on the server-side and I am able to get a document back, but for some reason I can't use the collection() like in the above example.

import { firestore } from '../utils/firebase'

export default defineEventHandler(async (event) => {
  const ref = firestore.doc(`animals/dog`)
  const snapshot = await ref.get()
  const data = snapshot.data()
  console.log(data)
  // return {
    // data
  // }
})

Also, if I run the collection() function on the client side, I can successfully retrieve the posts. I just can't figure out why it won't work server-side.

Anyone know why I get the above error?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

None of the server code you have shared align to such an error message. The error indicates that you are calling .get() or .onSnapshot() with something that is not a DocumentReference or a CollectionReference.

Note: collection() and doc() are functions that return References. They don't actually communicate to the FS service...a Reference is (essentially) a string such as `/posts' or '/posts/1234567'

So, somewhere in your code, you need to find where it is hitting the above error and either share it here or understand why what you are calling it on is not a valid CollectionReference or DocumentReference.

about 4 years ago · Juan Pablo Isaza 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