Start getting Error: 'Non-alphanum char in element with leading alpha:' Error when trying to update a doc

Viewed 1745

I've been doing it this way for a while.

Everything is working fine for the last two months and out of nowhere this error comes up, 'Non-alphanum char in element with leading alpha:' When trying to update on firestore. I don't even know where to begin looking for the issue.

# returns true if update was successful
def UpdateUser(self, user):
    try:
        # update will fail if it cant find an already existing document
        doc_ref = self._db.collection(USER).document(user._username)

        # update the values | this will fail if it doesn't exist
        doc_ref.update({
            u'Username': user._username,
            u'Password': user._password, 
            # u'Time Created': user._time_created,
            u'Last logged in': user._last_logged_in,
            u'Active': user._active,
            u'Phone': user._phone,
            u'Address': user._address,
            u'State': user._state,
            u'City': user._city,
            u'Zipcode': user._zipcode,
            u'Stripe Customer Id': user._stripe_customer_id,
            u'Email Activated': user._email_activated,
            u'Full Name': user._full_name,
            u'Id': user._id,
            u'Group Ids': user._group_ids,
        })
        return True
    except Exception as e:
        print(e)
        print("Failed to update {}".format(user._username))
        return False   

I expect the output to be a successful update, but it seems to throw an error.

3 Answers

i was struggling with the same error. i usually got this error whenever i ran the code shown below:

db.collection(''+collectionName).document(''+docId).update({'api-key':newkey})

it throws this error because i've got non alpha numeric char in key itself. Therefore i had to change the key in the firebase and change it in the code to this:

 db.collection(''+collectionName).document(''+docId).update({'apikey':newkey})

In your case, remove all the spaces for the keys(do the same in firebase) and try this:

doc_ref.update({
        u'Username': user._username,
        u'Password': user._password, 
        u'TimeCreated': user._time_created,
        u'Lastloggedin': user._last_logged_in,
        u'Active': user._active,
        u'Phone': user._phone,
        u'Address': user._address,
        u'State': user._state,
        u'City': user._city,
        u'Zipcode': user._zipcode,
        u'StripeCustomerId': user._stripe_customer_id,
        u'EmailActivated': user._email_activated,
        u'FullName': user._full_name,
        u'Id': user._id,
        u'GroupIds': user._group_ids,
    })

according to this answer you need to use field_path() on any string that includes a char that is not a letter or a number or an underscore. In your case you have spaces in your keys thus you should sanitize them using field_path. this code should fix your issue:

doc_ref.update({
    u'Username': user._username,
    u'Password': user._password, 
    # self._db.field_path(u'Time Created'): user._time_created,
    self._db.field_path(u'Last logged in'): user._last_logged_in,
    u'Active': user._active,
    u'Phone': user._phone,
    u'Address': user._address,
    u'State': user._state,
    u'City': user._city,
    u'Zipcode': user._zipcode,
    self._db.field_path(u'Stripe Customer Id'): user._stripe_customer_id,
    self._db.field_path(u'Email Activated'): user._email_activated,
    self._db.field_path(u'Full Name'): user._full_name,
    u'Id': user._id,
    self._db.field_path(u'Group Ids'): user._group_ids,
})

Please avoid using white spaces, special characters or non-aplhanumeric as keys in your dictionary. Better lowercase and use underscore for separator for readability.

doc_ref.update({
        u'user_name': user._username,
        u'password': user._password, 
        u'time_created': user._time_created,
        u'last_loggedin': user._last_logged_in,
        u'active': user._active,
        u'phone': user._phone,
        u'address': user._address,
        u'state': user._state,
        u'city': user._city,
        u'zip_code': user._zipcode,
        u'stripe_customer_id': user._stripe_customer_id,
        u'email_activated': user._email_activated,
        u'full_name': user._full_name,
        u'id': user._id,
        u'group_ids': user._group_ids,
    })
Related