Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

reactjs - How can I "mimic" a one to many relationship with react firebase cloud?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...