Firestore云功能,将作者的UID添加到文档创建中 [英] Firestore cloud function, add author's UID to document creation

查看:99
本文介绍了Firestore云功能,将作者的UID添加到文档创建中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的一个用户创建特定文档(例如帖子)时,我想将其UID添加到文档中.

When one of my users creates a specific document, for example a post, I would like to add its UID to the document.

我试图找到一种通过云函数和onCreate触发器实现此目标的方法,但似乎用户的UID在这些函数中无法访问.

I tried to find a way to achieve that through cloud functions and the onCreate trigger, but it seems that the user's UID isn't reachable within those function.

exports.createPublication = functions.firestore
    .document('publications/{publication}')
    .onCreate((snap, context) => {

      const newValue = snap.data();

      return snapshot.ref.child('authorId').set("ID");

    });

我真的很惊讶Firestore不会像其他数据库那样在本地添加AuthorId字段(Mongo,Parse等...) 当然,我可以从客户端添加此UID,但我认为它有点脏.

I was really suprised that Firestore doesn't add natively a AuthorId field like others databases would do (Mongo, Parse, etc...) Of course, I can add this UID from the client, but I think it is a bit dirty.

推荐答案

Firestore触发器当前无法知道进行了更改以触发触发器的已认证用户的uid.这是一项正在研究的功能.

Firestore triggers currently have no way to know the uid for the authenticated user that made the change that invoked the trigger. This is a feature that's being looked into.

如果文档ID恰好是用户的uid,则可以使用安全规则来限制对该用户的写访问,然后使用通配符来了解uid,但这听起来像是无法在此处使用.

If the document ID happens to be the uid of the user, you could use security rules to restrict write access to that user, then use wildcards to know the uid, but it sounds like that trick can't be used here.

这篇关于Firestore云功能,将作者的UID添加到文档创建中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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