简体   繁体   English

使用云函数向文档添加字段

[英]Add field to document using cloud functions

I wanted to make the id of each document as a field of that document so that I can store it inside the doc.我想将每个文档的 id 作为该文档的一个字段,以便我可以将它存储在文档中。 here is the cloud function I created:这是我创建的云函数:

 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})
})

this deploys without any errors but whenever I add a new document to the 'players' collection there is no change in the document whatsoever这部署没有任何错误,但是每当我将新文档添加到“玩家”集合时,文档中没有任何变化

In your question, you use the word "document" to describe your data, which is a Firestore concept, and you've tagged this question google-cloud-firestore.在您的问题中,您使用“文档”一词来描述您的数据,这是一个 Firestore 概念,并且您已将此问题标记为 google-cloud-firestore。 However, the code you've written is for Realtime Database, which is a completely different product.但是,您编写的代码用于实时数据库,这是一个完全不同的产品。 So it will never run in response to changes in Firestore.所以它永远不会响应 Firestore 中的变化而运行。

When you declare a function with functions.database , that's means Realtime Database.当您使用functions.database声明一个函数时,这意味着实时数据库。 Instead, you should declare a Firestore trigger with functions.firestore .相反,您应该使用functions.firestore声明一个Firestore 触发器 Please read the linked documentation for information about how to do that.请阅读链接的文档以获取有关如何执行此操作的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM