Get document collections in Firestore [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
this is my firebase database
- conversations (collection)
-- xxx (document)
--- users (collection)
---- xxx (document)
I want to list all the conversations and its users
this.db.collection('conversations').get().then(querySnapshot => {
querySnapshot.docs.forEach(doc => {
console.log(doc.collection('users').get())
});
});
I´m getting doc.collection is not a function
update: this is what I have after getting the id and making a new query. now the question is. is this performant?
this.db.collection('conversations').get().then(conversationsQuerySnapshot => {
conversationsQuerySnapshot.docs.forEach(doc => {
this.db.collection('conversations').doc(doc.id).collection('users').get().then(usersQuerySnapshot => {
usersQuerySnapshot.docs.forEach(doc => {
console.table(doc.data());
});
});
});
});

You're looking for doc.ref.collection('users'), since you need go get from the DocumentSnapshot to its DocumentReference through doc.ref.
Note that I find it easiest to spot such mistakes by simply following the types. Your querySnapshot is a QuerySnapshot, which means that your doc is a QueryDocumentSnapshot. Since QueryDocumentSnapshot doesn't have a collection method, it's easier to figure out what's going wrong.

you would call it: db.collection('conversations').doc({docID}).collection('users')
Your current query is set up to look through every conversation and then get every user in each of those. Plus the second part (getting the users) has no base document to pull from, which is why you're seeing an error.
I recommend watching this video

Related

How can I read data from firestore database in a schedule function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm new to Firebase and I'm blocked on something. Actually, I've some difficulties reading data from a Firestore Database. My code:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const db = admin.firestore();
exports.scheduledFunction = functions.pubsub.schedule("* * * * *").onRun(async () => {
console.log("start");
const querySnapshot = await db.collection("Next_callenges").get();
console.log("Let's see :", querySnapshot);
return null;
});
There is no output except this : "let's see : QuerySnapshot{".
To add some context, the objective behind this code is to get the first data inserted in the first database, add it to a second database and delete it from the first one.
As others have commented, the output you get is exactly what is expected from your code: since you log querySnapshot, you get whatever debug output the QuerySnapshot class defines.
If you want to see the data of each document in that query snapshot, you can do:
querySnapshot.forEach((doc) => {
console.log("Document "+doc.id+":", doc.data());
})
Note that this is just using the Firestore API and has nothing to do with the fact that you use Firestore in Cloud Functions. Since Cloud Functions adds quite some complexity to the case, I'd recommend first learning more about the Firestore API in JavaScript by reading its documentation and following its codelab.

Firestore - Cost when I get the parent document Id of the sub-collection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Would I be charged for a read when I get the parent document ID of the sub-collection ?
export const GetAllMySubCollectionWithFieldHello = async () => {
const query = await firebase.firestore().collectionGroup("MySubCollection").where("MyField", "==", "Hello").get();
query.forEach(doc => {
console.log(doc.id, ' => ', doc.data()); // Cost me one read for each document found
console.log(doc.ref.parent.parent.id); // This line will it cost me one read again ?
});
};
My understanding is that your .get will incur one read per document. I don't believe that the doc.data() method incurs any cost.
doc.ref.parent.parent.id shouldn't incur any cost. You are not reading anything from Firestore. You can run this command even when offline.

How do I fetch user data from firebase without being logged in? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I made a blog which has a database structure as shown below
users:{
$uid:{
Posts:{
$post_id: {...}
}
}
}
I'd like to have a public page where everybody can read the post without being logged in.
How do I fetch the data from firebase as it's clear I need the user id to go deeper
You should change the structure to the following:
users
uid
name : john
age : 100
posts
postId
userId : uid
post: post_content
Then you can do the following:
let ref = firebase.database().ref("posts");
ref.on("value", ((snapshot) => {
snapshot.forEach((childSnapshot) => {
let childData = childSnapshot.val();
console.log(childData.post);
});
});
childData.post will contain all the posts under posts node.

How to efficiently pull data out of a huge api with multiple pages [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am currently using an API to create a React Redux application. My problem is I am confused about how to efficiently use the data as the API has more than 14470 "pages" to navigate around. My main goal is to display certain items in the API based on key value pair. For example I would say I want to display a category in the API based on a certain key value pair such as "highestRating" and want to map through the API to find out the five items with the highestRating, how would I be able to do this efficiently?
What I have tried so far is looping to get the entire API available to me but then I get stuck with my current task at hand.
export const fetchHighestRating = () => async dispatch => {
let data = [];
let morePagesAvailable = true;
let currentPage = 0;
while (morePagesAvailable) {
currentPage++;
const response = await api.get(
`/api?page%5Blimit%5D=10&page%5Boffset%5D=${currentPage}`
);
data = [...data , response];
morePagesAvailable = currentPage < 17471;
}
dispatch({ type: FETCH_HIGHEST, payload: data });
};
This is not a javascript problem, it is database. You should run query directly in database to test response speed and decide what to do next, including:
Sharding
Indexing
Optimize config of DB
....
Any above required research in type of DB you are using, the current situation of DB so there is no exactly answer now!

userId.getUserById is not a function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
When a user click's on a username on the list below, it's meant to open up the add user page with the name's of the user in the fields.
I get this error message:
Here's the code in the manage users page:
componentWillMount: function () {
var userId = this.props.params.id; // from the path /user:id
if (userId) {
this.setState({ user: userId.getUserById(userId) });
}
},
Here's the code in the userApi:
getUserById: function(id) {
var user = _.find(users, {id: id});
return _clone(user);
},
I am new to StackOverflow and programming, so if this post doesn't meet the community's guidelines, please guide me on how to better make use this platform so I can further my learning progression.
Thanks,
Rickie
Using the userId variable you can't call your API. Since getUserById is from your API you must call the method from there.
userApi.getUserById(userId);

Categories

Resources