【问题标题】:Triggering a Cloud Function when a document is added to a Firestore collection将文档添加到 Firestore 集合时触发云函数
【发布时间】:2020-12-19 16:48:08
【问题描述】:

我目前正在尝试使用 Firestore、SwiftUI 和 Cloud Functions 集成支付处理器。

我目前有以下功能,它允许我将 Firestore 数据发送到处理器并获得返回,并将其保存到 Firestore。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

const Iyzipay = require('iyzipay');

const iyzipay = new Iyzipay({
  apiKey: 'sandbox-afXhZPW0MQlE4dCUUlHcEopnMBgXnAZI',
  secretKey: 'sandbox-wbwpzKIiplZxI3hh5ALI4FJyAcZKL6kq',
    uri: 'https://sandbox-api.iyzipay.com'
});

exports.pay = functions.https.onRequest((req, res) => {
  const docRef = db.collection('requests').doc('zFcWYdeP8wyLprYrc6o1');
  docRef.get().then(doc => {
      if (!doc.exists) {
        console.log('No such document!');
        return res.send('Not Found')
      } 
        console.log(doc.data());
        res.send(doc.data());

       const request = {
        locale: Iyzipay.LOCALE.TR,
        conversationId: '123456789',
        price: doc.data(doc).price,
        paidPrice: doc.data(doc).price,
        currency: Iyzipay.CURRENCY.TRY,
        installment: '1',
        basketId: 'B67832',
        paymentChannel: Iyzipay.PAYMENT_CHANNEL.MOBILE_IOS,
        paymentGroup: Iyzipay.PAYMENT_GROUP.LISTING,
        paymentCard: {
            cardHolderName: doc.data(doc).cardHolderName,
            cardNumber: doc.data(doc).cardNumber,
            expireMonth: doc.data(doc).expireMonth,
            expireYear: doc.data(doc).expireYear,
            cvc: doc.data(doc).cvc
        },
        buyer: {
            id: doc.data(doc).uid,
            name: doc.data(doc).name,
            surname: doc.data(doc).surname,
            gsmNumber: doc.data(doc).gsmNumber,
            email: doc.data(doc).email,
            identityNumber: doc.data(doc).identityNumber,
            registrationAddress: doc.data(doc).registrationAddress,
            city: doc.data(doc).city,
            country: doc.data(doc).country,
            zipCode: doc.data(doc).zipCode
        },
        shippingAddress: {
            contactName: doc.data(doc).name,
            city: doc.data(doc).city,
            country: doc.data(doc).country,
            address: doc.data(doc).registrationAddress,
            zipCode: doc.data(doc).zipCode
        },
        billingAddress: {
          contactName: doc.data(doc).name,
          city: doc.data(doc).city,
          country: doc.data(doc).country,
          address: doc.data(doc).registrationAddress,
          zipCode: doc.data(doc).zipCode
        },
        basketItems: [
            {
                id: doc.data(doc).productid,
                name: doc.data(doc).productname,
                category1: doc.data(doc).category1,
                itemType: Iyzipay.BASKET_ITEM_TYPE.PHYSICAL,
                price: doc.data(doc).price
            },
        ]
       }
       iyzipay.payment.create(request, function (err, result) {
        console.log(err, result);
        
        db.collection('results').doc(doc.data(doc).uid).set(result);
      }) 
      })
      })

我的目的是在将文档添加到 "requests" 集合时触发该函数并获取该文档,而无需像这样手动写入其 id:

const docRef = db.collection('requests').doc('zFcWYdeP8wyLprYrc6o1');

我尝试将functions.https.onRequest((req, res) 更改为functions.firestore.document('/requests/').onCreate((snap, context),但随后收到:

UnhandledPromiseRejectionWarning: ReferenceError: res is not defined

我该如何解决这个问题?谢谢。

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    Firestore triggers 没有与 HTTP 触发器相同的 API。如果您想编写 Firestore 触发器,则应按照文档中的说明进行操作。我建议开始一个全新的函数,让它适用于一个简单的案例,然后将你现有的代码移植到其中。

    请注意,Firestore 触发器没有 resreq 对象,因为没有与客户端应用程序的直接交互。您无法向应用程序发送响应,因为没有直接调用它的客户端应用程序。 Firestore 事件在写入完成后异步处理,并且应用继续执行其他操作。

    【讨论】:

      猜你喜欢
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2023-03-31
      • 2022-11-14
      • 1970-01-01
      相关资源
      最近更新 更多