goutham_mi3
goutham_mi3

Reputation: 345

Getting an 'str' object has no attribute '_max_attempts' error for cloud firestore transaction in python

Please anyone can resolve this error is much appreciated. Im using firebase_adminsdk and want to update the status of an order from my django project.

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

I am getting the below error always. Tried many ways. But could not solve this.

'str' object has no attribute '_max_attempts'

Upvotes: 3

Views: 850

Answers (1)

Bram Miedema
Bram Miedema

Reputation: 46

I have encountered the same issue as you. The way we fixed it was making sure the first argument in your function should be a transaction object created outside of the function.

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

It took some time at our side to figure this out since the examples do not show any real helpful documentation on this specific decorater. https://firebase.google.com/docs/firestore/manage-data/transactions

Upvotes: 3

Related Questions