小编tax*_*ree的帖子

如何使用 FirestoreDataConverter 进行更新?

我正在尝试使用FirestoreDataConverter来转换对象来存储数据,但转换器似乎仅适用于 和addDoc操作setDoc,但是当尝试将其与updateDoc操作一起使用时,它不会触发该toFirestore函数。

可重现的例子

interface Post {
  author: string;
  content: string;
}

async function addAndUpdatePost() {
  const colRef = collection(firestore, 'posts')
    .withConverter(postConverter);
  const post: Post = { author: 'Author', content: 'Content'};
  const docRef = doc(colRef);
  await setDoc(docRef, post);                      // Triggers toFirestore function
  const addedPost = (await getDoc(docRef)).data(); // Triggers fromFirestore function
  if (!addedPost) return;
  await updateDoc(docRef, addedPost)               // Does NOT trigger toFirestore function
}

const postConverter: FirestoreDataConverter<Post> = {
  toFirestore(post: Post): …
Run Code Online (Sandbox Code Playgroud)

firebase typescript google-cloud-firestore

8
推荐指数
1
解决办法
4243
查看次数