简体   繁体   English

云功能 firebase v9 runTransaction

[英]cloud functions firebase v9 runTransaction

this is my cloud function:这是我的云 function:

const { getFirestore, runTransaction, FieldValue } = require('firebase-admin/firestore')

exports.purchasesStatistics = functions.firestore
    .document('transactions/{purchaseId}')
    .onUpdate((snap, context ) => {
      if (snap.before.data().status === 'RECEIVED') {
        return '0'
      }
      let purchasePaid = snap.after.data().status === 'RECEIVED' ? true : false
      if (purchasePaid === false) {
        return '0'
      }
      let allPurchase = snap.after.data()
      functions.logger.log('allPurchase', allPurchase)
      let ref = getFirestore().collection('statistics').doc('checkout')
      return runTransaction(ref, (transaction) => {
        return transaction.get(ref).then((doc) => {
          functions.logger.log('documento atualizado:', doc.data())
          return '0'
        })
      })
    })

Buy, it's returning "runTransaction is not a function".购买,它返回“runTransaction 不是函数”。 What i'm doing wrong?我做错了什么? Didn't find proper way to use runTransaction on firebase v9没有找到在 firebase v9 上使用 runTransaction 的正确方法

It appears that you are using theadmin SDK for NodeJS.看来您正在为 NodeJS 使用管理员 SDK This SDK is initialized differently from the Web SDK V9 and the way you use the functions is also different.此 SDK 与 Web SDK V9 的初始化方式不同,您使用功能的方式也不同。 Here is a detailed guide on how to initialize the admin SDK from scratch.是有关如何从头开始初始化管理员 SDK 的详细指南。 After trying to add or import the runTransaction() function as your sample code, I also received the same error message.在尝试添加或导入runTransaction() function 作为您的示例代码后,我也收到了相同的错误消息。 I followed the getstarted guide in addition to the documentation example to properly use this function, by using it from the firestore object that is created:除了文档示例之外,我还按照入门指南正确使用了这个 function,方法是从创建的 firestore object 中使用它:

const { initializeApp, applicationDefault, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
const serviceAccount = require('/path-to-service-account'); //Path to your service account, depends on implementation

initializeApp({
    credential: cert(serviceAccount)
});

const fireDB = getFirestore();
fireDB.runTransaction(); //Use the relevant args

This additional page contains a different example for using transactions.此附加页面包含使用事务的不同示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM