将Firestore文档从Firebase触发函数复制到另一个集合。

huangapple go评论59阅读模式
英文:

Copy a firestore document to another collection from firebase trigger function

问题

I want to sync my doc to another collection based on the firebase trigger function.

我想根据 firebase 触发函数 将我的文档同步到另一个集合。

exports.foo = functions
  .region("foo")
  .firestore.document("foo")
  .onWrite((events, context) => {
    return new Promise((resolve, reject) => {
      if (events.after.exists) {
        // on create or update
        admin
          .firestore().doc("newFoo")
          .set(events.after.data)
          .then(() => { resolve(true); })
          .catch((ex) => { reject(ex); });
      } else{
        // on delete
        admin
          .firestore()
          .doc("newFoo")
          .delete()
          .then(() => { resolve(true); })
          .catch((ex) => { reject(ex); });
      }
    });
  });

但是当我尝试在 Firestore 中手动编辑我的文档时,我收到以下错误:

> Error: Value for argument "data" is not a valid Firestore document.
> Input is not a plain JavaScript object.

为什么会出现这个错误?根据我的检查,events.after.data 也返回了一个 FirebaseFirestore.DocumentData,这是 doc.set() 的正确数据类型。

英文:

I want to sync my doc to another collection based on the firebase trigger function

exports.foo = functions
  .region("foo")
  .firestore.document("foo")
  .onWrite((events, context) => {
    return new Promise((resolve, reject) => {
      if (events.after.exists) {
        // on create or update
        admin
          .firestore().doc("newFoo")
          .set(events.after.data)
          .then(() => { resolve(true); })
          .catch((ex) => { reject(ex); });
      } else{
        // on delete
        admin
          .firestore()
          .doc("newFoo")
          .delete()
          .then(() => { resolve(true); })
          .catch((ex) => { reject(ex); });
      }
    });
  });

but I got the following error when I try to manually edit my document in firestore

> Error: Value for argument "data" is not a valid Firestore document.
> Input is not a plain JavaScript object.

why do I get the error? as I check, the events.after.data is also returning a FirebaseFirestore.DocumentData which is the correct data type for doc.set()

答案1

得分: 1

.set(events.after.data) 应该改为 .set(events.after.data()),因为 data 是一个方法而不是属性。

英文:

This line .set(events.after.data) should be .set(events.after.data()) as data is a method not a property.

huangapple
  • 本文由 发表于 2023年5月13日 17:10:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定