有没有一种写法->一次性读取Firestore中新添加的文档?我一直在写,然后进行第二次查询以读取它,并且在它起作用的同时,我想知道下面是否有替代方法。我在SO上也发现了类似的问题,但其中许多问题都已过时。
const someAsyncFunc = async () => {
try {
const lesson = await userLessonRef.get();
if (lesson.exists) {
// ? if lesson exists, return it.
return lesson.data();
}
// ? if lesson does NOT exist, create it.
await firebaseFirestore
.collection(`users/${userID}/languages/${language}/lessons`)
.doc(lessonID)
.set({ greetings: 'hello' });
// ? return the newly created lesson.
const newLesson = await firebaseFirestore
.collection(`users/${userID}/languages/${language}/lessons`)
.doc(lessonID)
.get();
return newLesson.data();
} catch (error) {
console.log('Error finding lesson collection', error);
}
}