Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

409
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda