简体   繁体   English

Firebase Cloud函数-如何查询集合,对其进行迭代,然后为每个文档运行函数?

[英]Firebase Cloud Functions - How to query a collection, iterate over it and then run functions for each document?

With as prolific as Firebase is, I'm a bit surprised to find that I can't seem to find much documentation on how to query a collection for a set of documents (of which the ID is not known), and then perform some logic based on properties of each those documents. 与Firebase一样多产,令我感到惊讶的是,我似乎找不到太多关于如何查询集合中一组文档(其ID未知)的文档,然后执行一些操作。逻辑基于每个文档的属性。

In my particular example, all I'm trying to do is query a collection of pending payments based on if the charge date has passed and then process the charge with Stripe. 在我的特定示例中,我要做的就是根据收费日期是否过去来查询待付款项的集合,然后使用Stripe处理收费。 So far, I haven't had any luck in running the function and I get this error: 到目前为止,我在运行该函数时没有任何运气,但出现此错误:

TypeError: functions.firestore.collection is not a function
    at exports.chargePendingStripeAccounts.functions.pubsub.schedule.onRun (/srv/lib/index.js:78:32)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:127:23)
    at /worker/worker.js:825:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

Here is my code for the function 这是我的功能代码

exports.chargePendingStripeAccounts = functions.pubsub.schedule('every 2 minutes').onRun((context) => {

  return functions.firestore.collection('payments', (ref) => ref.where('charge_date', '>=', new Date())).get()
    .then(payments => {

      payments.forEach(doc => {
        const data = doc.val();
        const amount = data.price * 100;
        const idempotency_key = data.creator_id;  // prevent duplicate charges
        const source = data.token.id;
        const currency = 'USD';
        const charge = {amount, currency, source};

        return stripe.charges.create(charge, { idempotency_key });
      })

    });
});

Surely there's a way to do this in Cloud Functions, right? 当然,在Cloud Functions中可以找到一种方法,对吗?

It looks like you're trying to use the Cloud Functions for Firebase SDK to make a query to Cloud Firestore. 您似乎正在尝试使用Cloud Functions for Firebase SDK来查询Cloud Firestore。 That's not going to work. 那是行不通的。 The Functions SDK (in your code, identified by functions ) just lets you declare triggers that run in response to changes in your project. Functions SDK(在您的代码中,由functions标识)仅允许您声明触发以响应项目中的更改而运行的触发器。 If you want to query Cloud Firestore, you will have to use one of the server SDKs available for nodejs. 如果要查询Cloud Firestore,则必须使用可用于nodejs的服务器SDK之一。 You can use the SDK provided by Google Cloud , or you can use the Firebase Admin SDK , which just wraps the Cloud SDK. 您可以使用Google Cloud提供SDK ,也可以使用Firebase Admin SDK (仅包装Cloud SDK)。

Pretty much all of the official samples provided by the Firebase team use the Admin SDK where appropriate, so you can use those as examples. Firebase团队提供的几乎所有正式示例在适当的地方都使用Admin SDK,因此您可以将其用作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM