【问题标题】:How to pass additional parameter in firebase transaction method in Python如何在 Python 中的 firebase 事务方法中传递附加参数
【发布时间】:2022-02-11 21:11:38
【问题描述】:

我正在尝试为实时 firebase 数据库创建这样的事务:

tranRef = db.reference('all_items')
new_transRef = tranRef.transaction(updateDatabase)

我的 updateDatabase 函数如下所示:

def updateDatabase(current_value):
    print(type(current_value))
    return current_value

这里的 current_value 是包含根“all_items”的所有子节点的数据。这工作正常。
我想要的是向 updateDatabase 传递一个额外的参数,比如 cartList,它是一个字典列表。 我该怎么做? 我本质上想要的是一个看起来像这样的函数:

def updateDatabase(current_value, cartList):
    print(type(current_value))
    return current_value

在调用函数时我应该如何传递列表:

tranRef = db.reference('all_items')
new_transRef = tranRef.transaction( ## what should I write here ##)

【问题讨论】:

    标签: python firebase-realtime-database


    【解决方案1】:

    我也在处理同样的问题,我使用lambda解决了它:

    extra_param = ['your cart list here']
    tranRef = db.reference('all_items')
    new_transRef = tranRef.transaction(lambda current_value: updateDatabase(current_value, extra_param))
    

    然后你可以像这样调用事务方法:

    def updateDatabase(current_value, cartList):
        print(type(current_value))
        return current_value
    

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 2022-08-02
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多