【问题标题】:Add field to document using cloud functions使用云功能向文档添加字段
【发布时间】:2020-04-01 22:21:27
【问题描述】:

我想将每个文档的 id 作为该文档的一个字段,以便我可以将其存储在文档中。这是我创建的云函数:

 exports.assignPID = functions.database
.ref('/players/{playerId}')
.onCreate((snapshot,context)=>{
    const playerId = context.params.playerId;
    console.log("new player "+playerId);

    // const data = snapshot.val();
    return snapshot.ref.update({'pid': playerId})
})

这部署没有任何错误,但每当我将新文档添加到“玩家”集合时,文档中没有任何变化

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    在您的问题中,您使用“文档”一词来描述您的数据,这是一个 Firestore 概念,并且您已将此问题标记为 google-cloud-firestore。但是,您编写的代码是针对实时数据库的,这是一个完全不同的产品。所以它永远不会响应 Firestore 的变化而运行。

    当您使用functions.database 声明函数时,这意味着实时数据库。相反,您应该用functions.firestore 声明Firestore trigger。请阅读链接文档,了解如何执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 1970-01-01
      相关资源
      最近更新 更多