【问题标题】:Start getting Error: 'Non-alphanum char in element with leading alpha:' Error when trying to update a doc开始出现错误:'Non-alphanum char in element withleading alpha:' 尝试更新文档时出错
【发布时间】:2019-05-25 03:25:00
【问题描述】:

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

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

# 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   

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

【问题讨论】:

    标签: python google-cloud-firestore


    【解决方案1】:

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

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

    它会引发此错误,因为我在键本身中有非字母数字字符。因此,我不得不更改 firebase 中的密钥并在代码中将其更改为:

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

    在您的情况下,删除键的所有空格(在 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,
        })
    

    【讨论】:

      【解决方案2】:

      根据这个answer,您需要在任何包含不是字母、数字或下划线的字符的字符串上使用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,
      })
      

      【讨论】:

        猜你喜欢
        • 2018-03-22
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多