【问题标题】:Firestore Cloud write functions not workingFirestore Cloud 写入功能不起作用
【发布时间】:2019-01-11 13:21:32
【问题描述】:

我正在尝试设置在完成条带付款时触发的承诺链。我的函数正确 lint 和编译但它们没有部署但是当我尝试 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);
      })
  })  

如果我能帮我弄清楚问题出在哪里以及为什么我的触发器没有被保存,我将不胜感激

【问题讨论】:

  • 不是 100% 肯定......但我怀疑你的触发器的 /store{userId}/mypayments 部分。我不记得曾经见过任何使用这样的“部分”通配符的触发器,并且快速的谷歌搜索没有发现任何东西。你从哪里得知/store{userId}/ 没问题?我只见过通配符路径的整个部分:/{storeuserId}/...尝试通配符整个事情/{storeuserId}/
  • 我这样说是因为我试图将用户的付款历史保存在他的数据库下。我像这样将每个用户保存在数据库中,例如(store15hskav456gakbfyql/mypayments/activepayments/gavfjdn2495gagfk)将是一条路径
  • @user10112707 是的,我明白你想要做什么,我只是从来没有见过它这样做。你是从某个地方得到的,还是你自己编造的?如果你编造了,那么我敢打赌这就是你的突破点……试着把整个东西变成通配符,看看它是否能正确部署。
  • @JeremyW 的发现似乎是正确的,我怀疑是否支持部分片段。尝试使用常规路径.document('/store/{userId}/mypayments/activepayments/{paymentId}')。如果部署(当然它不会工作),你知道是什么导致了问题。顺便说一句,杰里米:我可能会将其发布为答案。

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


【解决方案1】:

根据我的(和其他人)上面的 cmets,共识是您作为触发器观看的路线无效 - 我认为 Firebase 不支持“部分" 像/store{userId}/ 这样的通配符...我明白你想要做什么,我只是不认为它受支持。

尝试改变你的触发器来观看

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

并且应该部署。您必须改变计划存储信息的方式,但触发器会起作用。

【讨论】:

  • 你是对的。 firebase 不支持部分通配符我通过增加.document('users/{userId}/{storeuserId}/mypayments/activepayments/{paymentId}') 之类的路径解决了这个问题,所以我可以从其他地方获取 userId 并且仍然从不同位置获得商店 ID。非常感谢您的帮助
猜你喜欢
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2020-10-15
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多