2017-10-15 101 views
1

如何通过由onCreate()云触发器触发的云功能为新创建的文档添加新属性。 是否可以使用相同的方法在客户端以及服务器端升级文档,即在云功能中?Google Cloud Firestore触发器

回答

2

the docs,您可以使用event.data.ref执行操作:

exports.addUserProperty = functions.firestore 
    .document('users/{userId}') 
    .onCreate(event => { 
    // Get an object representing the document 
    // e.g. {'name': 'Marie', 'age': 66} 
    var data = event.data.data(); 

    // add a new property to the user object, write it to Firestore 
    return event.data.ref.update({ 
     "born": "Poland" 
    }); 
}); 
+0

我选择了自动生成的ID为documents.Then如何添加路径为特定的文件,因为我不知道文档ID –

+0

这是通配符:“{wildcardId}”的用途。它将捕获该位置的所有更改。 –