简体   繁体   中英

i can't personally todos react native firestore

I made a todo app using firebase. I store the todos in firestore and save the email address of the user who added the todo into the document. but when a user saves todo, all users see that todo. my codes:

const [dailys, setDailys] = useState([]);
const dailyRef = firebase.firestore().collection('daily');
const [addDaily, setAddDaily] = useState('');
const email = firebase.auth().currentUser.email;

useEffect(() => {
    async function check() {
        dailyRef.orderBy('createdAt', 'desc')
            .onSnapshot(
                querySnapShot => {
                    const dailys = []
                    querySnapShot.forEach((doc) => {
                        const { heading } = doc.data()
                        dailys.push({
                            id: doc.id,
                            heading,
                            email: email,
                        })
                    })
                    setDailys(dailys)
                }
            )
    }
    check()
}, [])
const addDailyPlan = () => {
    if (addDaily && addDaily.length > 0) {
        const timeStamp = firebase.firestore.FieldValue.serverTimestamp();
        const daily = {
            email:email,
            heading: addDaily,
            createdAt: timeStamp
        };
        dailyRef
            .add(daily)
            .then(() => {
                setAddDaily('');
                Keyboard.dismiss();
            })
            .catch((error) => {
                alert(error);
            })
    }
}
render(
 <FlatList data={dailys}/>

)

If everyone is pushing in the same collection, everyone will have the same result...

Either create a specific collection for each user (recommended), or add this to your query => where("email", "==", email)

Read the docs for reference

const email = firebase.auth().currentUser.email;
const dailyRef = firebase.firestore().collection('daily');

 setDoc(doc(dailyRef,email ), {
    name:"name",
    email:email });

const q = query(dailyRef, where("email", "==", email));

I followed your advice and used a code in the above structure. but I am getting an error like this: [Unhandled promise rejection: TypeError: t._freezeSettings is not a function. (In 't._freezeSettings()', 't._freezeSettings' is undefined)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM