【问题标题】:Specify numeric field value at creation in Firestore?在 Firestore 中创建时指定数字字段值?
【发布时间】:2018-11-17 20:22:41
【问题描述】:

我想在创建时在 firestore 数据库中创建一个整数值。但是,我传递的每个变量都会变成一个字符串。

这就是我所拥有的:

next_group_id = self.GetNextGroupId() #returns int value
try:
# get the document
    doc_ref = self._db.collection(GROUPS).document(next_group_id)      

    # create a new document  
    # this gets created as a string in firestore    
    doc_ref.set({
        u'Group Id': next_group_id, 
    })
    print("Created Group '{}'.".format(group._group_name))
except:
    print("Possible firestore error has accord - unable to create group")

【问题讨论】:

  • 如果您对值进行硬编码,它设置的数字是否正确? IE。 doc_ref.set({ u'Group Id': 12, })。如果是这样,至少很明显问题出在客户端/Python 端。
  • 我收到此错误路径元素必须是字符串。收到 102,这是一个 .
  • 嗯……这听起来很奇怪。我不确定为什么会这样,所以希望其他人注意到。

标签: python-3.x firebase google-cloud-firestore firebase-admin


【解决方案1】:

原来你不能用这样的整数搜索文档:

doc_ref = self._db.collection(GROUPS).document(next_group_id) 

所以我只是转换为一个字符串,它工作正常......

doc_ref = self._db.collection(GROUPS).document(str(next_group_id))   

【讨论】:

    猜你喜欢
    • 2015-08-17
    • 2018-04-15
    • 2020-11-05
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2020-10-01
    相关资源
    最近更新 更多