![](/img/trans.png)
[英]Can't load firestore document data to variable in React-Native
[英]i can't personally todos react native firestore
我使用 firebase 做了一個 todo 應用程序。我將 todos 存儲在 firestore 中,並將添加 todo 的用戶的 email 地址保存到文檔中。 但是當用戶保存待辦事項時,所有用戶都會看到該待辦事項。 我的代碼:
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}/>
)
如果每個人都推同一個集合,每個人都會有相同的結果......
為每個用戶創建一個特定的集合(推薦),或者將其添加到您的查詢中 => where("email", "==", email)
閱讀文檔以供參考
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));
我聽從了您的建議並使用了上述結構中的代碼。 但我收到這樣的錯誤:[Unhandled promise rejection: TypeError: t._freezeSettings is not a function. (In 't._freezeSettings()', 't._freezeSettings' is undefined)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.