【发布时间】:2021-11-09 01:44:24
【问题描述】:
每次我刷新页面时,都会在 Firestore 数据库中进行一个新查询,以向用户显示(有时)相同的数据。
这是一个书柜。下面的代码只读取已经添加的数据:
export default {
name: "Shelf",
date: () => ({ shelfName: "", books: [] }),
async mounted() {
const auth = getAuth();
const db = getFirestore();
const userID = auth.currentUser.uid;
const userRef = doc(db, "users", userID);
const userSnap = await getDoc(userRef);
userSnap.exists()
? (this.shelfName = "Shelf: " + userSnap.data().name)
: (this.shelfName = "Your Shelf");
const userBooksRef = query(collection(db, "users", userID, "addedBooks"));
const querySnapshot = await getDocs(userBooksRef);
querySnapshot.forEach((doc) => {
this.books.push({
id: doc.id,
addedIn: doc.data().addedIn,
readIn: doc.data().readIn,
});
});
this.books.map(async (book) => {
const booksRef = doc(db, "books", book.id);
const bookSnap = await getDoc(booksRef);
book.authors = bookSnap.data().authors;
book.title = bookSnap.data().title;
book.thumbnail = bookSnap.data().thumbnail;
});
},
};
我不确定是否应该寻找缓存解决方案,或者是否有其他类型的解决方案。
下面我展示了books 集合。重要的是要说在 users 集合中有一个名为 addedBooks 的集合,它引用了下面每本书的 id:
【问题讨论】:
-
这就是全局存储 (Vuex) 的用途。
标签: firebase vue.js caching google-cloud-firestore vuejs3