how to fetch data of all collections from firebase in react - javascript

I am trying to fetch data from the firebase. I have two collections, and I want data from both collections to be printed. When I fetch, only data from one collection is shown
const fetchData = () => {
const arr = []
db.collection('storedata').get().then((querysnapshot) => {
querysnapshot.forEach((element) => {
const dataSet = element.data()
if (email !== '' && password !== '') {
return arr.push(setInfo([dataSet]))
}
})
})
}

You are fetching data from the collection 'storedata', using:
db.collection('storedata').get()
If you want to do the same but with other collection you need to explicitly write the same code for whatever other collection you are trying to fetch!
To achieve what you want to achieve you need to know the name of the other collection and repeat the same process you made for 'storedata' collection
You can't, and should not try to fetch all documents in the Firestore database.
But if you absolutely need for it to happen, then it's better to make it explicit by fetching each collection one by one.

Related

How to join two collections in firebase with array of ids using Angular?

I am new to firebase and I am trying to get data from firebase. In MySQL, this is very easy but I don't know how can we join collections in firebase. Please guide me to solve this small issue.
Below is the data in my firebase.
I want to get match the team ID with the users' collection. Below is my code with where condition but it is not working.
getProjects() {
this.firestore.collection('projects').valueChanges().subscribe(projects => {
// console.log(projects)
// this.projects = projects;
const teamRef = this.firestore.collection('users');
projects.forEach(project => {
teamRef.where('')
})
// console.log(this.projects)
})
}
Any solution Appreciated!

Firebase Firestore collection could not retrieve - react native

Context: I have a list of doctors on Firebase Firestore and I want to retrieve data related to them.
Problem: App is behaving like collection is empty but it currently contains three doctors. I want to retrieve data in list of objects so I can access them in app. Also, if you have any other idea of retrieving the data let me know, since I haven't found any that works for me.
Here is the problematic part of code:
componentDidMount(){
firebase.firestore().collection('doctors')
.get()
.then((querySnapshot
) => {
querySnapshot.forEach(snapshot => {
const doctors = []
snapshot.forEach(doc => {
const data = doc.data()
data.id = doc.id;
doctors.push(data)
})
this.setState({doctors :doctors})
})})
}
Here is how my Firebase looks like:
I found this snippet which I believe it's your exact use case:
// retrieve a collection
db.collection('documents')
.get()
.then(querySnapshot => {
const documents = querySnapshot.docs.map(doc => doc.data())
// do something with documents
})
You may find this example, and others here. Under each snippet, you can find the original source in case you want more context for each snippet.

Conditinal database record delete firebase

I want to clean some records of a firestore database. I have a function that receives the records I would like to keep, I check in all the records if id is not in the docsToKeep:firebase documents argument.
I use a foreach for all the records, and I user filter to find the matching id as shown in the working snippet below:
async function checkForNotifClean(docsToKeep:firebase.firestore.QuerySnapshot<firebase.firestore.DocumentData>) {
const db = firebase.firestore();
const notifCollection = db.collection('notifications')
const allData = await notifCollection.get();
allData.docs.forEach(doc => {
const filtered = docsToKeep.docs.filter(entry => entry.id === doc.id);
const needsErase = filtered.length === 0;
if (needsErase) {
const id = doc.id;
notifCollection.doc(id).delete();
}
})
}
Is there a cleaner way to erase all the documents that are not in docsToKeep ? Would be kind of function that get all the objects that are not in both arrays and delete those. I would be happy on improvements on the javascript side to filter out the records to delete, and in the firebase side, improvements regarding the chance of deleting some set of records at once instead of looping through all and deleting each.
To delete (or write to) a document in Firestore you need to know the complete path to that document, including its document ID. If you don't have the IDs of the documents to delete, then your only option is to determine those IDs as you're doing now.
It does lead to the question how docsToKeep is populated though. My guess it that the user already selects some documents from a full list. If that's the case, might you be able to pass the inverted list: docsToDelete?

Saving a field from firestore that is an array

I am currently working on a mobile app on React and I am having trouble understanding how to save a field from fire store that is an array.
Since I can't post images my database structure is all strings such as username, first name, etc but I have a field called follow list that is an array.
What I want to do is save the usernames from the following list into an array to later search fire store for the username's in the array, this is basically what I want to do so I can render my app's social Feed. I do know that I can probably create another subcollection and write something familiar to what I did to search for users but that was a QuerySnapShot which was overall documents not a specific one and I also know firebase creates an ID for arrays and it increases as the array gets bigger.
I do not want to end up making two more subcollections one for followers and following which I think not ideal right? My current approach is this
export const fetchUserFollowing = async (username) => {
const ref = firebase.firestore().collection('users').doc(username)
let results = []
ref
.get()
.then( doc => {
let data = doc.data()
results = data
})
.catch((err) => {
return 'an error has occurred ', err
})
}
From what I understand is that a DocumentSnapShot .get() function returns an object but what I want is to store follow list into results and then return that but I am not sure how to manipulate the object return to just give me follow List which is an array
https://rnfirebase.io/docs/v5.x.x/firestore/reference/DocumentSnapshot
link to docs

How to combine document id and values in firestore

I am fetching data from firestore and displaying those data's in a list.But the response which i am getting from firestore contains the document id separately and values separately. At the time of fetching itself i am combining it in a array. I would like to know whether it is the correct way of doing or is there any way we can do it better to combine the document id and values together.
Steps which i have done for combining:
db.getEmployees().then((snapshot) => {
let employee = [];
snapshot.docs.forEach((doc) => {
let user = doc.data();
user.id = doc.id;
employee.push(user);
});
dispatch(employeeUpdateSuccess(employee));
According to the documentation for a DocumentSnapshot, there is no way to get the data and the id in a combined way (i.e. with only one method or via one property). In other words, you will always have to use the two following lines of code:
... = doc.data();
... = doc.id;

Categories

Resources