请任何人都可以解决此错误,我们深表感谢。我正在使用firebase_adminsdk,并希望更新我的Django项目中的订单状态。
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate(settings.FIREBASE_CONFIG_PATH)
firebase_admin.initialize_app(cred)
dbFire = firestore.client()
@firestore.transactional
def updateFirestore(docId, status, assignedTo, profileURL):
fireTransaction = firestore.client.transaction()
docRef = dbFire.collection(u'orders').document(settings.ORDERS_CONFIG).collection(u'orders').document(docId)
snapshot = docRef.get(transaction=fireTransaction)
data = {
"status":status,
"status_code":orderStatuses[status],
"assignedTo":assignedTo,
"profileURL":profileURL,
"dateupdated": datetime.datetime.now()
}
try:
# docRef.update(data)
res = fireTransaction.update(docRef, data);
print('Transaction result: ', res)
return True
except firebase_admin.exceptions.FirebaseError as fe:
logger.error(msg='Firebase error: updateFirestore: {}. Order details: {}, {}'.format(fe, docId, data))
return False
except Exception as e:
logger.error(msg='Unknown error: updateFirestore: {}. Order details: {}, {}'.format(e, docId, data))
return False
我总是收到以下错误。尝试了很多方法。但这无法解决。
'str' object has no attribute '_max_attempts'
答案 0 :(得分:3)
我遇到了与您相同的问题。我们解决问题的方法是确保函数中的第一个参数应该是在函数外部创建的事务对象。
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate(settings.FIREBASE_CONFIG_PATH)
firebase_admin.initialize_app(cred)
dbFire = firestore.client()
transaction = dbFire.transaction() # needs to be set for the transaction context
# this expects the first argument to be the transaction
@firestore.transactional
def updateFirestore(transaction, docId, status, assignedTo, profileURL):
fireTransaction = firestore.client.transaction()
docRef = dbFire.collection(u'orders').document(settings.ORDERS_CONFIG).collection(u'orders').document(docId)
snapshot = docRef.get(transaction=fireTransaction)
data = {
"status":status,
"status_code":orderStatuses[status],
"assignedTo":assignedTo,
"profileURL":profileURL,
"dateupdated": datetime.datetime.now()
}
try:
# docRef.update(data)
res = fireTransaction.update(docRef, data);
print('Transaction result: ', res)
return True
except firebase_admin.exceptions.FirebaseError as fe:
logger.error(msg='Firebase error: updateFirestore: {}. Order details: {}, {}'.format(fe, docId, data))
return False
except Exception as e:
logger.error(msg='Unknown error: updateFirestore: {}. Order details: {}, {}'.format(e, docId, data))
return False
我们花了一些时间弄清楚这一点,因为这些示例并未显示有关此特定装饰器的任何真正有用的文档。 https://firebase.google.com/docs/firestore/manage-data/transactions