Node.js 必须适当处理Firebase条带(错误)承诺
我试图用firebase和stripe处理我的付款,在尝试将我的功能部署到云时遇到了一个问题,即“承诺必须得到适当处理”。我知道这是一个tlint编译错误,但不知道为什么会触发该错误 这是我的密码Node.js 必须适当处理Firebase条带(错误)承诺,node.js,firebase,google-cloud-functions,Node.js,Firebase,Google Cloud Functions,我试图用firebase和stripe处理我的付款,在尝试将我的功能部署到云时遇到了一个问题,即“承诺必须得到适当处理”。我知道这是一个tlint编译错误,但不知道为什么会触发该错误 这是我的密码 import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; admin.initializeApp(functions.config().firebase); const str
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
const stripe = require('stripe')(functions.config().stripe.testkey);
exports.stripeCharge = functions.firestore
.document('/payments/{userId}/mypayments/{paymentId}')
.onCreate((snap,event) => {
const payment = snap.data()
const userId = event.params.userId
const paymentId = event.params.paymentId
// 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(customer => {
const amount = payment.price * 100 // amount must be in cents
const idempotency_key = paymentId // prevent duplicate charges
const source = payment.token.id
const currency = 'usd'
const charge = {amount, currency, source}
return stripe.charges.create(charge, { idempotency_key })
})
.then((charge) => {
admin.firestore()//The error keeps referring me to this line
.collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
.set({
charge: charge
}, { merge: true })
})
})
产生错误的那一行如上所述最后找出它
无论何时创建promise函数,它都必须以错误处理程序结束,因此我使用了一个简单的catch
.then((charge) => {
admin.firestore()
.collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
.set({
charge: charge
}, { merge: true })
.catch(er=>{
console.log(er);
return er
}
)
})
终于弄明白了
无论何时创建promise函数,它都必须以错误处理程序结束,因此我使用了一个简单的catch
.then((charge) => {
admin.firestore()
.collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
.set({
charge: charge
}, { merge: true })
.catch(er=>{
console.log(er);
return er
}
)
})
实际上,使用最新版本的云函数,您不必在承诺链接中包含
catch()
。云函数运行的平台将自行处理错误
根据这篇文章,这显然是由TsLint(EsLint?)产生的错误
然而,与TsLint检测到的这个“错误”无关,我认为您可能会遇到云功能问题,因为您没有返回链的最后承诺:
return admin.firestore() //HERE YOU RETURN CORRECTLY
.doc(`/users/${userId}`)
.get()
.then(snapshot => {
return snapshot //HERE YOU RETURN CORRECTLY
})
.then(customer => {
const amount = payment.price * 100 // amount must be in cents
const idempotency_key = paymentId // prevent duplicate charges
const source = payment.token.id
const currency = 'usd'
const charge = {amount, currency, source}
return stripe.charges.create(charge, { idempotency_key }) //HERE YOU RETURN CORRECTLY
})
.then((charge) => {
return admin.firestore() //HERE, IN YOUR CODE, YOU DON'T RETURN
.collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
.set({
charge: charge
}, { merge: true })
})
})
实际上,使用最新版本的云函数,您不必在承诺链接中包含
catch()
。云函数运行的平台将自行处理错误
根据这篇文章,这显然是由TsLint(EsLint?)产生的错误
然而,与TsLint检测到的这个“错误”无关,我认为您可能会遇到云功能问题,因为您没有返回链的最后承诺:
return admin.firestore() //HERE YOU RETURN CORRECTLY
.doc(`/users/${userId}`)
.get()
.then(snapshot => {
return snapshot //HERE YOU RETURN CORRECTLY
})
.then(customer => {
const amount = payment.price * 100 // amount must be in cents
const idempotency_key = paymentId // prevent duplicate charges
const source = payment.token.id
const currency = 'usd'
const charge = {amount, currency, source}
return stripe.charges.create(charge, { idempotency_key }) //HERE YOU RETURN CORRECTLY
})
.then((charge) => {
return admin.firestore() //HERE, IN YOUR CODE, YOU DON'T RETURN
.collection('/payments').doc(userId).collection('mypayments').doc(paymentId)
.set({
charge: charge
}, { merge: true })
})
})