Trying to get all the collection data from Firestore - javascript

I am getting the following Error while trying to get all the data of a Collection from a Firestore
I have a collection names users in my Firestore DB
I am using Firebase Web version 9 (Modular)
const querySnapshot = await getDocs(collection(db, "users"));
querySnapshot.forEach((doc) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.data());
});
I am using the code from the Official documentation and still getting that error
https://firebase.google.com/docs/firestore/query-data/get-data#web-version-9_6
still this is what i get
TypeError: (0 , lib_firebase__WEBPACK_IMPORTED_MODULE_4__.collection) is not a function

Have you used the this line in your code -
import { collection, getDocs } from "firebase/firestore"; because without importing the collection function won't be accessible and can generate the error.
There are a few possible reasons for this error:
You might have imported the wrong module or a module that does not contain the collection function. Make sure you have imported the Firebase library correctly and that you are using the correct version of the library.
You might have mistyped the name of the function. Make sure you are using the correct spelling and capitalization of the function name.
You might be trying to use the collection function on an object that is not a Firestore instance. The collection function is a method of the Firestore class, so you need to make sure you are calling it on a Firestore instance.

Related

Why does .get() result in TypeError: posts.get is not a function?

I'm trying to console log a query of all the available documents within the Posts collection in firebase. Can someone explain why .get() is not a function when I'm literally using the same code from firebase docs?
const posts = collection(db, 'Posts');
async function getAllDocs() {
const snapshot = await posts.get();
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data());
});
}
You're mixing two versions of the Firebase API that are not compatible with each other.
Your first line uses the newer v9 modular API, while the like that is failing is trying to use the older v8 namespaced API. The two can't be mixed like that.
If you want to use the v9 modular API, the failing line should be:
const snapshot = getDocs(posts);
When doing this type of development I always keep the Firebase documentation handy, as it has side-by-side snippets of the two API versions. For example, this specific case is covered in the documentation on getting all documents in a collection.

How to get access from one collection to another collection in firebase

how to get access from one collection to another collection in firebase in JS v9
Firebase's JS API v9 brought different changes.
One of the biggest changes is the fact that the DocumentReference don't allow the access to subcollections anymore. Or at least, not directly from the DocumentReference itself, how we used to to with v8.
In v8, for example, we could do something like this:
//say we have a document reference
const myDocument = db.collection("posts").doc(MY_DOC_ID);
//we can access the subcollection from the document reference and,
//for example, do something with all the documents in the subcollection
myDocument.collection("comments").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
// DO SOMETHING
});
});
With v9, we have a different approach. Let's say we get our document:
const myDocument = doc(db, "posts", MY_DOC_ID);
As you can note, the way we write the code is different. In v8 we used to write it in a procedural way. With v9, everything switched to a more functional way, where we can use functions such as doc(), collection() and so on.
So, in order to do the same thing we did with the above example and do something with every doc in the subcollection, the code for v9 API should look like this:
const subcollectionSnapshot = await getDocs(collection(db, "posts", MY_DOC_ID, "comments"));
subcollectionSnapshot.forEach((doc) => {
// DO SOMETHING
});
Note that we can pass additional parameters to functions such as collection() and doc(). The first one will always be the reference to the database, the second one will be the root collection and from there onward, every other parameter will be added to the path. In my example, where I wrote
collection(db, "posts", MY_DOC_ID, "comments")
it means
go in the "posts" collection
pick the document with id equals to MY_DOC_ID
go in the "comments" subcollection of that document

Using react native with firestore get().data() returns undefined

I'm just wondering what's wrong with the way I'm fetching data
using firebase with react-native
why does this work
const res = await firestore().collection('users').doc(uid).get();
const data = res.data().todos.todo.work
but this way I get undefined
const res = await firestore().collection('users').doc(uid).collection('todos').doc('todo').get();
const data = res.data().work
I also tried this but got undefined as well
const res = await firestore().collection('users').doc(`${uid}/todos/todo`).get();
const data = res.data().work
what exactly am I missing?
Without seeing your database, I can only guess that you only have a single document and no actual subcollections. It looks like your single document has nested fields call "todo" inside it. A nested field in a document can't be fetched individually using special syntax. You can only read an entire document at once.
Nested fields in Firestore are not subcollections. They are just fields within a single document. The syntax in the second code sample that doesn't work is attempting to read a document in a subcollection, which simply doesn't exist.
I would suggest reviewing the documentation to better understand how fields and subcollections work, and the code you would use to access either one.

Firebase FieldPath.documentId() where query issue

I am trying to query my firebase db using the below code but keep getting docuemtnID is undefined. Is my syntax way off as I keep getting Cannot read property 'documentId' of undefined
db.collection("listings")
.where(db.FieldPath.documentId(), "==", element.drawId)
.get()
.then(docRef => {
});
Firebase is imported as import { db } from "../firebase/init";
You can't access FieldPath as a property of the db instance. Without seeing what's in ../firebase/init, it's not possible to say for sure what you need to use instead (as we can't see how you imported/required the Firestore module). In JavaScript web clients, you would typically say firebase.firestore.FieldValue.
But it's worth noting that since queries using FieldPath.documentId() are only ever going to return a single document with the given ID, you could just get() it instead, and be much more clear and concise:
db.collection("listings").doc(element.drawId).get().then(...)
See: Get a document

How to fix "Property 'getAll' does not exist on type 'typeof firestore'"

I'm getting a list of documents using an array of document IDs using firestore.getAll(). The problem is that I can't call getAll() since it doesn't seem to exist in firestore nor AngularFire, or I may be doing something wrong.
Thank you in advance for your help and/or direction!
firebase.firestore.getAll(
[].concat(this.arrayOfUIDs).map(uid => this.firestore.doc(this.ref + uid))
Firebase is imported as:
import * as firebase from 'firebase';
And .getAll() also does not exist in AngularFirestore
Expected result: Return an object containing a list of documents whose IDs exist in the arrayOfUIDs
Result: error in getAll() "Property 'getAll' does not exist on type 'typeof firestore'"
firestore doc: https://cloud.google.com/nodejs/docs/reference/firestore/1.3.x/Firestore#getAll
getAll() is a method provided by the node SDK. Check carefully the link to the API docs that you provided. getAll() is not currently available for web clients, though that might change in the future. If you want to fetch multiple documents by IDs, you will have to make a different query for each one.

Categories

Resources