从 Cloud Functions for Firebase 中的数据库触发器获取用户 ID? [英] Getting the user id from a database trigger in Cloud Functions for Firebase?

查看:35
本文介绍了从 Cloud Functions for Firebase 中的数据库触发器获取用户 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中,有没有办法获取写到/messages/{pushId}/original 的用户的 uid?

In the example below, is there a way to get the uid of the user who wrote to /messages/{pushId}/original?

exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
  // Grab the current value of what was written to the Realtime Database.
  const original = event.data.val();
  console.log('Uppercasing', event.params.pushId, original);
  const uppercase = original.toUpperCase();
  // You must return a Promise when performing asynchronous tasks inside a Functions such as
  // writing to the Firebase Realtime Database.
  // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
  return event.data.ref.parent.child('uppercase').set(uppercase);
});

推荐答案

自从 Firebase 函数达到 1.0 版以来,这种行为不再没有记录,而是略有改变.请务必阅读文档.

Ever since Firebase functions reached version 1.0, this behavior is no longer undocumented but has sligtly changed. Be sure to read the docs.

已将上下文添加到云功能中,您可以像这样使用它

Context has been added to cloud functions and you can use it like this

  exports.dbWrite = functions.database.ref('/path/with/{id}').onWrite((data, context) => {
  const authVar = context.auth; // Auth information for the user.
  const authType = context.authType; // Permissions level for the user.
  const pathId = context.params.id; // The ID in the Path.
  const eventId = context.eventId; // A unique event ID.
  const timestamp = context.timestamp; // The timestamp at which the event happened.
  const eventType = context.eventType; // The type of the event that triggered this function.
  const resource = context.resource; // The resource which triggered the event.
  // ...
});

这篇关于从 Cloud Functions for Firebase 中的数据库触发器获取用户 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆