Cloud Firestore函数Javascript:使用whereEqualTo查询触发函数 [英] Cloud Firestore Function Javascript: Triggering a function with whereEqualTo query

查看:33
本文介绍了Cloud Firestore函数Javascript:使用whereEqualTo查询触发函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,我们可以通过设置这样的查询来在包含简单查询/复合查询的集合内进行监听.

In Android we can listen inside a collection with simple queries/compound queries by setting a query like this.

mQuery = mDatabase.collection("Users")
            .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .collection("Request").whereEqualTo("status","received")
            .whereEqualTo("type","friend_request")
            .orderBy("time", Query.Direction.DESCENDING)
            .limit(5);

然后基于这样的查询设置侦听器.

Then setting a listener based on that Query like this.

 mQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
            //If something went wrong
            if (e != null)
                Log.w(TAG, "ERROR : ", e);...//foreach loop

我可以使用Cloud Function做类似的事情吗?我目前正在使用这一行代码,并且可以正常工作.

Can I do similar things with Cloud Function? I am currently using this line of code and it is working.

functions.firestore.document("Users/{user_id}/Notifications/{notification_id}").onCreate((added, context) => {

但是对于某些更改,现在我只想触发一个函数,即文档字段等于我认为的foreach循环?

But for some changes, now I would like to trigger a function only where document field is equal to something then foreach loop I think?

另一件事是,如果可能进行查询,我现在将如何获取通知"集合的文档ID,现在我使用通配符,然后像这样检索它.

Another thing is how am I going to get the document Id of 'Notification' collection now if queries is possible, now I use wild card then retrieving it like this.

const notification_id = context.params.notification_id;

推荐答案

使用Firestore的Cloud Functions触发器,您只能使用通配符指定导致该功能触发的集合和文档.没有可用的查询过滤器.如果要进一步限制哪些更改的文档需要采取某些措施,则必须在代码中检查该文档的内容.

With Cloud Functions triggers for Firestore, you can only specify which collections and documents cause the function to fire by using wildcards. There are no query filters available. If you want to further restrict which changed documents require some action, you have to check the contents of the document in your code.

这篇关于Cloud Firestore函数Javascript:使用whereEqualTo查询触发函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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