【问题标题】:Error when adding documents to firestore using add() method使用 add() 方法将文档添加到 Firestore 时出错
【发布时间】:2019-09-25 03:03:17
【问题描述】:

我正在尝试使用 add() 方法将文档添加到 Firestore。根据文档add() 方法为每个文档生成一个唯一的 ID 并以任意随机顺序存储它并以有序方式存储它,我们需要在文档中的 timestamp 字段中存储它。以下是我的做法:

def add_data(message):
  message.update({u'timestamp': firestore.SERVER_TIMESTAMP})
  coll_ref.add(message)

当我执行上述方法时出现错误:

TypeError: ('Cannot convert to a Firestore Value', <object object at 0x104fdebe0>, 'Invalid type', <class 'object'>)

为什么会出现此错误? firestore Python SDK 中是否存在错误,如果有,那么是否已针对此问题发布任何补丁?

【问题讨论】:

    标签: python firebase google-cloud-firestore firebase-admin


    【解决方案1】:

    使用最新版本的 Python Admin SDK(在 REPL 中验证)对我有用。

    >>> import firebase_admin
    >>> from firebase_admin import firestore
    >>> firebase_admin.initialize_app()
    <firebase_admin.App object at 0x1080c33d0>
    >>> client = firestore.client()
    >>> msg = {'timestamp': firestore.SERVER_TIMESTAMP}
    >>> foo = client.collection('foo')
    >>> foo.add(msg)
    (seconds: 1569404958
    nanos: 428866000
    , <google.cloud.firestore_v1.document.DocumentReference object at 0x108dfc610>)
    

    这在下面使用google-cloud-firestore 的 v1.4.0。检查您的库版本、导入等。

    【讨论】:

    • 我正在使用 google-cloud-firestore 的 v0.31.0。我当时是对的,是因为版本不正确。如果我打算坚持使用 v0.31.0,是否有任何解决方法?
    • 不太可能。是什么阻止您升级?
    • 管理限制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多