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

324
Vistas
React Native: firestore/firebase Expected first argument to collection() to be a CollectionReference... how?

So I have a simple React Native app that I created using Expo and debug on my personal Android device.

I've included firebase/firestore into my app and am now trying to add an object to firestore on button click.

Here's my code:

firebaseConfig.js :

import { initializeApp } from '@firebase/app';

var config = {
    ...
}
  
const app = initializeApp(config);

export default app;

Component:

import { app } from '../firebaseConfig'; import { getFirestore } from 'firebase/firestore/lite'; import { doc, setDoc, collection, query, where, getDocs, initializeFirestore } from "firebase/firestore"; ...

export default function Component() { const firestoreDb = getFirestore(app);

// this function is called when clicking a button
const onAddListPress = () => {
    setDoc(doc(firestoreDb, "cities", "LA"), {
        name: "Los Angeles",
        state: "CA",
        country: "USA"
    });
}

}

This throws the following error:

Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore

This code above (in the onPress) is copied from the official firestore docs, here: https://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document

Does anyone have an idea why this does not work?

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

0

I suggest you edit your post and remove the firebaseConfig configuration keys, or delete your Firebase app and create a new one from the Firebase console to secure your app. I tried to replicate your application using React, and I received the same error message. There were two changes I made to correct the error. The first one is inside the firebaseConfig file, the default export needs to be changed to a named export:

import { initializeApp } from "firebase/app"

const firebaseConfig = {
    //Project configuration keys
};

const app = initializeApp(firebaseConfig);

export { app }; // Change from a default to a named export

In addition, all the imports inside your component should come from the full Javascript SDK, not the lite version. The lite version has some limitations which can result in this specific error.

import { app } from "./firestore-config";
import { getFirestore, getDoc, doc } from "firebase/firestore";
import './App.css';

export default function App() {
  const firestoreDB = getFirestore(app);
  const docRef = doc(firestoreDB, "users", "testUser1");

  const getData = async () => {
    const data = await getDoc(docRef);
    console.log(data);
  }

  getData();
  return <div className="App"></div>;
}

You might also want to review this GitHub issue if you are using yarn to manage your dependencies.

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