小编典典

从Cloud Functions for Firebase中的数据库触发器中获取用户ID?

javascript

在下面的示例中,是否有办法获取写给/ messages / {pushId} / original的用户的uid?

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

阅读 239

收藏
2020-05-01

共1个答案

小编典典

自从Firebase功能达到1.0版以来,这种行为就不再没有记载,而已进行了重大更改。请务必阅读文档

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

  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.
  // ...
});
2020-05-01