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}/>
)
CodePudding user response:
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
CodePudding user response:
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)]