开始出现错误:“带有前导 alpha 的元素中的非 alphanum 字符:”尝试更新文档时出错

Dyl*_*ite 8 python google-cloud-firestore

我这样做已经有一段时间了。

过去两个月一切正常,当尝试在 firestore 上更新时,突然出现此错误:“带有前导 alpha 的元素中的非字母字符:”。我什至不知道从哪里开始寻找问题。

# 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   
Run Code Online (Sandbox Code Playgroud)

我希望输出是成功的更新,但它似乎抛出一个错误。

OmG*_*G3r 5

根据这个答案,您需要在任何包含非字母、数字或下划线的字符的字符串上使用field_path() 。在您的情况下,您的键中有空格,因此您应该使用 field_path 清理它们。这段代码应该可以解决您的问题:

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,
})
Run Code Online (Sandbox Code Playgroud)


小智 1

我正在为同样的错误而苦苦挣扎。每当我运行下面所示的代码时,我通常会收到此错误:

db.collection(''+collectionName).document(''+docId).update({'api-key':newkey})
Run Code Online (Sandbox Code Playgroud)

它抛出此错误,因为我的键本身有非字母数字字符。因此我必须更改 firebase 中的密钥并将其在代码中更改为:

 db.collection(''+collectionName).document(''+docId).update({'apikey':newkey})
Run Code Online (Sandbox Code Playgroud)

在您的情况下,删除键的所有空格(在 firebase 中执行相同操作)并尝试以下操作:

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,
    })
Run Code Online (Sandbox Code Playgroud)