我正在将我的应用程序链接到cloude,并为此使用python,我需要检索值"Balance“字段,以检查它是否有效。但是当我试图检索它时,我得到了这样的输出
{ Balance: 80 }但是我只需要余额的整数/浮点值。我的代码如下-
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# Use the application default credentials
cred = credentials.Certificate("/Users/admin/Downloads/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
user_id= 'my user id in database'
u = 80
userbal_new= u-10
users_ref = db.collection(u'users').document(user_id)
#update balance
#users_ref.update({u'Balance': userbal_new})
#get balance
get_bal= users_ref.get({u'Balance'})
bal = u'{}'.format(get_bal.to_dict())
print (bal)发布于 2019-03-22 07:52:52
因为您在这里使用的是to_dict函数:
bal = u'{}'.format(get_bal.to_dict())这将生成一个字典,之后您可以调用bal dict,如下所示:
bal['Balance']或者,以格式进行调用(如果总是需要平衡的话):
bal = u'{}'.format(get_bal.to_dict()['Balance'])发布于 2019-12-16 15:51:32
可以通过传递field_paths在快照中与db.collection().document().get()一起指定要返回的field_paths={}。
get_bal = users_ref.get(field_paths={'Balance'}).to_dict()这将只返回字典中的余额:
{ Balance: 80 }我觉得读起来很容易。所以如果你只想要整数。
bal = get_bal.get('Balance')或
bal = users_ref.get(field_paths={'Balance'}).to_dict().get('Balance')会回来
80发布于 2019-03-22 07:56:36
如果您正在获取bal ={ ' balance ':80 },则用于获取余额。印刷品(“平衡”)
https://stackoverflow.com/questions/55295060
复制相似问题