【发布时间】: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