I created a todoApp using firebase firestore. Authentication with Firebase I did. But when I add a todo, all other users see that todo. I want to create a personal todo list. my codes:
export default function DailyScreen({ navigation }) {
const [dailysTodo, setDailysTodo] = useState([]);
const dailyRef = firebase.firestore().collection('daily');
const [addDailyTodo, setAddDailyTodo] = useState('');
useEffect(() => {
async function check(){
dailyRef.orderBy('createdAt', 'desc')
.onSnapshot(
querySnapShot => {
const dailys = []
querySnapShot.forEach((doc) => {
const { heading } = doc.data()
dailysTodo.push({
id: doc.id,
heading,
})
})
setDailysTodo(dailys)
}
)
}
check()
}, [])
const addDailyPlan = () => {
if (addDailyTodo && addDailyTodo.length > 0) {
const timeStamp = firebase.firestore.FieldValue.serverTimestamp();
const daily = {
heading: addDaily,
createdAt: timeStamp
};
dailyRef
.add(daily)
.then(() => {
setAddDailyTodo('');
Keyboard.dismiss();
})
.catch((error) => {
alert(error);
})
}
}
return(
<FlatList
data={dailysTodo}
renderItem={({ item }) => (
<View>
// todo items
</View>
)}
/>
)
I want every user to see their own todo list when they enter the app. But when a user adds todo, all users see that todo
2
Answers
You’ll need to first associate each todo with a specific user, so that you can then load the todos for a specific user.
There are two common ways of doing that in Firestore:
dailyRef.where('uid', '==', 'theUidValueToLoad').onSnapshot...
/users/$uid/todos
. With this you can then load the todos with something likefirebase.firestore().collection('users').doc('theUidValueToLoad').collection('todos').onSnapshot...
In addition to Frank’s answer, note this is a database so you’ll want to secure the data server side via Firebase rules. See https://firebase.google.com/docs/rules