I was trying to use a database in my project, so i imported 'collection' to use it this is my code:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js";
import { getAuth, createUserWithEmailAndPassword, signOut, signInWithEmailAndPassword, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-auth.js";
import { getFirestore, collection } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-firestore.js";
const firebaseConfig = {
apiKey: "AIzaSyC3GLIN5TBmCDoTfy0dEOgOdvVvqNw-ric",
authDomain: "auth-project-38aaa.firebaseapp.com",
projectId: "auth-project-38aaa",
storageBucket: "auth-project-38aaa.appspot.com",
messagingSenderId: "431888894254",
appId: "1:431888894254:web:71bb9b250fbb8a21edd2bf",
measurementId: "G-6BBPCJ3814"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
// get data
collection(db, 'guides').get().then(snapshot => {
console.log(snapshot.docs);
});
but instead of showing me the 'snapshot.docs' in the console it gives me this message:
auth.js:20 Uncaught TypeError: collection(...).get is not a function
at auth.js:20
please help me to solve this problem.
You're importing the new modular/functional SDK, where the equivalent of collectionRef.get() is actually:
getDocs(collection(db, 'guides')).then(snapshot => {
...
The Firebase documentation itself has all been updated to contain both the older snippets, and snippets for the new API, such as in this case the documentation on getting multiple documents.
But if you're new to Firebase and using code from other places, those might not have both snippets styles yet. In that case it might be easier to start by importing the compat libraries from the new SDKs, while you're figuring out how everything works. If you just want to do that for Firestore, it'd be:
import 'firebase/compat/firestore';