I'm trying to learn how to use Firebase by creating a simple social media application.
https://i.stack.imgur.com/wfeyw.png (Here is how my database is laid out)
I was able to add data to the database but I'm having trouble understanding how to retrieve data. I want to be able to retrieve all the posts from a certain user and then display each one as an item in a list.
let Posts = firebase.database().ref('Posts').child(userId);
So far thats my query and here is the code I'm using to add the posts to the database. Could someone also tell me what is the term for the Id that is being generated for each post?
firebase.database().ref('/Posts').child(userId).push({
Text: post,
Score: 0
});
Related
I am working on a web app for doing certain problems. The database is firestore.
The feature I want to ask is that on a page, there will show {x}/{y},
where x is the number of problems the user completed, and y is the total number of problems, something like 6/10.
Sample image
My doubt is how to design data model to achieve this feature. I already have users collection and problems collection. My thought is choosing one of them, but feeling none of them is good practice:
A. Adding user_response field in each problem doc, which means if there are 1000 users, each problem needs to add 1000 users' responses. It is also hard for new users coming.
B. Quite like A but adding problems_response into user doc.
C. Adding a new collection repsonses which has problem_id, user_id fields.
I am new to NoSQL, thanks for your help.
I added the database structure in the following images:
database-1
database-2
database-3
database-4
database-5
This question already has answers here:
How can I write a firebase rule to allow reading only part of a collection/document? [duplicate]
(1 answer)
How to allow only particular fields of a firestore document to be accessed publicly
(2 answers)
Firestore security rules for public and private fields
(2 answers)
Closed 2 years ago.
I want to get a Document from Firestore, but just with some fields of the doc.
I found 2 use cases for this. One of these is:
Users in Firestore
UsersCollection
UserDocument
email
uid
photoUrl
name
If I want to make a chat with all of the users in the users collection, I need to get all the docs in the user collection. But these docs contain some important info that's not useful to our chat, like the e-mail. Can't I just request from firebase the user document without the e-mail field? Can't a hacker find the user document in memory and get their e-mail? The e-mail is not useful for the chat, I only need the name, uid and photoUrl, the e-mail is no use for this feature, but I still get it with the whole document.
How do I "except" or get the document without the e-mail? This is more about security and user privacy.
Another use case for this would be a like system. Let's say we have a post structured like this in firebase:
PostsCollection
PostDocument
number of likes
arrayOfUsersThatLiked
photoUrl
number of likes is incremented by a cloud function that when arrayOfUsersThatLiked is modified, the function adds the length of the array in the number of likes field.
(arrayOfUsersThatLiked is used to not let the user like more than 1 time, so if someone liked the post, arrayOfUsersThatLiked will keep track of who and will not let them like again)
When we get the post document from the db, we will actually get that usersThatLiked array with a lot of uids along with the things that we need, we only need the photoUrl and the number of likes on the post, not the usersThatLiked array. (it would be a pain also to store such a huge string array).
So the main question remains, how do I only get the document with only the fields that I want to? It's a matter of privacy/security and performance.
With the Client SDKs (including the FlutterFire plugin) it is not possible to get only a subset of the fields for a Document. When you fetch a Document you get it with all its fields.
Here are three possible approaches:
Dernomalize your data: You create another collection which contains documents that only have the fields you want to display in the front end. The complexity is that you need to keep the two collections in sync: when you create/modify/delete a doc in the master collection, you need to update the other collection. This can be done from your front-end (e.g. you write to the two collections in a batched write) or with a Cloud Function, triggered in the back-end with an onWrite trigger. In terms of security, you open read access only to the second collection, not to the master one.
Use the Firestore REST API: With the REST API you can use a DocumentMask when fetching documents, which will "restrict a get operation on a document to a subset of its fields. ". Note, however, that a "hacker" could get the collection ID and fetch the documents without the Mask.
Use a Cloud Function to access the Firestore documents. With a Cloud Function, you have full control on what you want to send back to the front-end, since you query the collection from within the Cloud Function and build the response in the Cloud Function. However, using a Cloud Function instead of a Client SDK has several drawbacks: see this article for more details.
I want to make a small project using react js in which one can sign up/ sign in and then make a post on the web app. I have made the authentication component, but I have trouble displaying only the posts that were made by the logged user. (currently, it shows all posts from the database)
My current db is like this:
user
-email
-displayName
post
-text
-image
My current approach was the following, where db is:
db = firebaseApp.firestore();
for the posts:
db.collection("posts").add({
text: message,
image: media
});
and for the users:
db.collection("users").add({
email: email
});
I was thinking of changing the db structure, like so:
user
-email
-displayName
-text1
-image1
...
However, it is not possible to dynamically set the keys' names, and I don't know how I can store the post and then access it via code.
You can try structure your database like this
user
email
displayName
post
creator
text
image
The creator field contain user's ID that is the owner of the post. When you want to fetch only the post created by that user, you can use where to filter that out. Here is the reference https://firebase.google.com/docs/firestore/query-data/queries
I have a contacts collection (contacts of all employees) in my angular app. It is similar to any contacts or address app where there are contacts and you can search for a name. When the search is done for a name like 'John', the app should show only names having 'John'.
The contacts are saved already in the MSSQL DB.
My question is about how should I manage the state with #ngrx store?
Should I read all the contacts from the MSSQL DB when the app starts and then do the name search in the #ngrx store with a reselect selector to get the search results?
Should I do the search in the MSSQL DB and then clear and load the #ngrx store with the search results every time when the search is done?
2nd option, of course.
there is no point in downloading all the data.
you should have searchResults part of the store, update it on every search and do store.select(reducer.getSearchResults) to show it in your components.
if you would follow the same logic throughout the whole app with loading every piece of data you could ever need in your app lifetime you would end up with useless app very quickly ;)
Just started using ionic, angular and firebase. And I'm having an issue creating a master-detail pattern.
What I want to achieve:
Have a list on my page that's being retrieved from firebase database. When clicking on of the listitems it should show the details of the selected listitem.
What I currently have:
I can save data in firebase database through a form. This is my write operation that I want to get and put it in my list. I'm adding the data in the database with push() method of firebase
My problem:
I've flattened the data structured as described in the documentation
MetaData{
2017:{
1x48sdf3617SDf542394:{
name: "someName",
date: "24/01/2017"
}
}
},
fullData{
2017:{
8i89gslsdk617SDf542gsLd:{
name: "someName",
date: "24/01/2017",
comment: "someComment",
required: "true",
...
}
}
}
So in the list, which is the masterpage, I only getting the metaData back. When clicking it should go to the detail page. The detailpage should get the data back from fullData. The problem is how is the mapping between the metadata and the fulldata. because they both have a unique key generated by firebase.
I looked at some other questions, but they don't have this specific issue as they are defining their own unique key. I don't want to do that, would like to use the auto generated key from firebase.
How would I tackle this?
You can achieve your requirement in two possible ways :
Using the same key for metadata and details. You can store the key in a variable and use the same to write the details. your data structure be like
/metadata/someKey/ {your javascript object}
/detail/someKey/ {your detail of metadata}
By referencing it from metadata. In this case your data structure will be
/metadata/someKey/{....., details : someKey1}
/details/someKey1
I found the answer in the documentation. I need to use the update() method. First creating a key, then using that key in multiple places to save the data using the data() method
Check the documentation