NodeJS 为什么Firebase无法检测ProjectId?

h4cxqtbf  于 2023-03-29  发布在  Node.js
关注(0)|答案(1)|浏览(98)

Firebase无法检测项目ID,但我给予了serviceAccount信息,我没有找到解决方案。可能这里是语法错误,因为我真的不知道Firebase管理包。
错误如下所示:
无法在当前环境中检测到项目ID。

import { buffer } from 'micro'
import { initializeApp, getApps, getApp, cert} from 'firebase-admin/app'
import { getFirestore  , FieldValue } from 'firebase-admin/firestore'

const serviceAccount = require('../../permissions.json')

const app = !getApps().length
  ? initializeApp({
      credentials: cert(serviceAccount),
    })
  : getApp()

const db = getFirestore(app)

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)

const endpointSecret = process.env.STRIPE_SIGNING_SECRET 

const fullfillOrder = async (session) => {
  return db
    .collection('users')
    .doc(session.metadata.email)
    .collection('orders')
    .doc(session.id)
    .set({
      amount: session.amount_total / 100,
      amount_shipping: session.total_details.amount_shipping / 100,
      images: JSON.parse(session.metadata.images),
      timestamp: FieldValue.serverTimestamp()
    })
}

export default async function webhook(req, res) {
  if (req.method === 'POST') {
    const reqBuffer = await buffer(req)
    const payload = reqBuffer.toString()
    const sig = req.headers['stripe-signature']

    let event

    try {
      event = stripe.webhooks.constructEvent(payload, sig, endpointSecret)
    } catch (err) {
      console.log(err.message)
      return res.status(400).send(`Error : ${err.message}`)
    }

    if (event.type === 'checkout.session.completed') {
      const session = event.data.object

      return fullfillOrder(session)
        .then(() => res.status(200))
        .catch((err) => {
          res.status(400)
          console.log(`Webhook error ${err.message}`)
        })
    }
  }
}
iezvtpos

iezvtpos1#

原因是初始化应用程序设置对象接收credentialL,而不是credentialS。

相关问题