Firestore Cloud写入功能不起作用

时间:2019-01-11 13:21:32

标签: firebase google-cloud-firestore google-cloud-functions

我正在尝试建立一连串的承诺,这些承诺会在完成条纹付款后触发。我的函数可以正常运行并编译,但是没有部署,但是当我尝试firebase deploy --only functions时遇到此错误

+  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (37.79 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: updating Node.js 6 function stripeCharge(us-central1)...
i  functions: updating Node.js 6 function getTime(us-central1)...
+  functions[getTime(us-central1)]: Successful update operation.
!  functions[stripeCharge(us-central1)]: Deployment error.
Failed to configure trigger providers/cloud.firestore/eventTypes/document.create@firestore.googleapis.com 
(__gcf__.us-central1.stripeCharge)


Functions deploy had errors. To continue deploying other features (such as 
database), run:
firebase deploy --except functions

Error: Functions did not deploy properly.

这是我的函数代码

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);

const stripe = require('stripe')('mytestkey');

exports.stripeCharge = functions.firestore
   .document('/store{userId}/mypayments/activepayments/{paymentId}')
   .onCreate((snap, event) => {
     const payment = snap.data()
     const userId = event.params.userId
     const paymentId = event.params.paymentId
     console.log(payment);

   // checks if payment exists or if it has already been charged
   if (!payment || payment.charge) return null;

   return admin.firestore()
      .doc(`/users/${userId}`)
      .get()
      .then(snapshot => {
       return snapshot
      })
      .then(snapshot => {
        const amount = payment.amount // amount must be in cents
        const idempotency_key = paymentId  // prevent duplicate charges
        const source = payment.token.source.id;
        const currency = 'usd'
        const charge = { amount, currency, source }
        console.log(charge);

        return stripe.charges.create(charge, { idempotency_key })
      })
      .then((charge) => {
        admin.firestore()
          .doc(`store${userId}/mypayments/activepayments/${paymentId}`)
          .set({
            charge: charge 
          }, { merge: true })
          .then(() => {
            if (charge.status === 'succeeded') {
              if (payment.amount === 3000) {
                const validTill = Date.now() + 12 * 30 * 24 * 60 * 60 * 1000;
                admin.firestore()
                  .doc(`/store${userId}/stats/mystatistics/exp`)
                  .set({
                    validTill
                  }).then(() => {
                    console.log('successfully updated expiration date from server');
                  }
                  )
                  .catch(er => {
                    console.log(er);
                     return er;
                   })
               }
              if (payment.amount === 2000) {
                const validTill = Date.now() + 6 * 30 * 24 * 60 * 60 * 1000;
                admin.firestore()
                  .doc(`/store${userId}/stats/mystatistics/exp`)
                  .set({
                    exp: validTill
                  },{merge: false})
                  .catch(er => {
                    console.log(er);
                    return er;
                 })
              }
             if (payment.amount === 5100) {
                const validTill = Date.now() + 12 * 30 * 24 * 60 * 60 * 1000
                admin.firestore()
                   .doc(`/store${userId}/stats/mystatistics/exp`)
                   .set({
                   exp: validTill
                  },{merge: false})
                   .catch(er => {
                    return er;
                  })
              }
               if (payment.amount === 2700) {
               const validTill = Date.now() + 6 * 30 * 24 * 60 * 60 * 1000
                admin.firestore()
                  .doc(`/store${userId}/stats/mystatistics/exp`)
                  .set({
                    exp: validTill
                   },{merge: false})
                  .catch(er => {
                    return er;
                  })
              }  
              if (payment.amount === 500) {
                const validTill = Date.now() + 30 * 24 * 60 * 60 * 1000
                admin.firestore()
                 .doc(`/store${userId}/stats/mystatistics/exp`)
                 .set({
                    exp: validTill
                  },{merge: false})
                  .catch(er => {
                    return er;
                  })
               }
            }
           }).catch(er =>{
             console.log(er)
         })
       })
      .catch(er=>{
         console.log(er);
      })
  })  

我非常感谢您提供任何帮助来找出问题的根源以及为什么不保存触发器的原因

1 个答案:

答案 0 :(得分:2)

根据我上面的(和其他人)的评论,共识是您正在观看的触发路由无效-我不认为Firebase支持“ 部分“ /store{userId}/之类的通配符...我明白了您要尝试执行的操作,但我认为它不受支持。

尝试更改触发器以观看

.document('/{storeUserId}/mypayments/activepayments/{paymentId}')

,应该部署。您必须更改计划的信息存储方式,但是触发器将起作用。