升级到firebase-functions 1.0.1后解析函数触发器时出错

时间:2018-04-19 21:20:45

标签: firebase google-cloud-firestore

我试图在1.0版本发布后更新我的firebase功能。我已阅读1.0.1迁移文档(https://firebase.google.com/docs/functions/beta-v1-diff)并尽最大努力进行必要的更改,但在尝试部署时发生错误。

更新前:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

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

const stripe = require('stripe')(functions.config().stripe.token)

exports.stripeCharge = functions.firestore
  .document('/users/{userId}/payments/{paymentId}')
  .onWrite(event => {
    const payment = event.data.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

    return admin.firestore()
      .doc(`/users/${userId}`)
      .get()
      .then(snapshot => {
        return snapshot
      })
      .then(customer => {
        const amount = payment.amount
        const idempotency_key = paymentId  // prevent duplicate charges
        const source = payment.token.id
        const currency = 'usd'
        const description = 'irl Map Fine Print'
        const charge = {amount, currency, source}

        return stripe.charges.create(charge, { idempotency_key })
       })
       .then(charge => {
         admin.firestore()
          .doc(`/users/${userId}/payments/${paymentId}`)
          .set({
            charge: charge
          }, { merge: true })
       })
   })

更新后:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp()

const stripe = require('stripe')(functions.config().stripe.token)

exports.stripeCharge = functions.firestore
  .document('/users/{userId}/payments/{paymentId}')
  .onWrite((change, context)) => {
    const payment = change.after.data()
    const userId = context.params.userId
    const paymentId = context.params.paymentId

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

    return admin.firestore()
      .doc(`/users/${userId}`)
      .get()
      .then(snapshot => {
        return snapshot
      })
      .then(customer => {
        const amount = payment.amount
        const idempotency_key = paymentId  // prevent duplicate charges
        const source = payment.token.id
        const currency = 'usd'
        const description = 'Map download'
        const charge = {amount, currency, source}

        return stripe.charges.create(charge, { idempotency_key })
       })
       .then(charge => {
         admin.firestore()
          .doc(`/users/${userId}/payments/${paymentId}`)
          .set({
            charge: charge
          }, { merge: true })
       })
   })

部署时,出现以下错误:

    Error: Error occurred while parsing your function triggers.

/Users/astrojams1/Apps/jhames-map/map/functions/index.js:8
exports.stripeCharge = functions.firestore
                                ^

SyntaxError: Unexpected token .
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:21:11

这是我的package.json:

    {
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "^5.12.0",
    "firebase-functions": "^1.0.1",
    "stripe": "^5.4.0"
  },
  "private": true
}

1 个答案:

答案 0 :(得分:1)

此行中的函数存在语法错误:

.onWrite((change, context)) => {

context后你有一个额外的括号。如果我纠正了,我没有编译错误。