python - 无法在 Firebase 查询数据中传递变量

标签 python firebase google-cloud-firestore

根据 https://firebase.google.com/docs/firestore/query-data/get-data我正在尝试在 python 中使用下面的代码并在查询中传递变量。

举个例子

docs = db.collection(u'cities').where(u'capital', u'==', True).get()

for doc in docs:
    print(u'{} => {}'.format(doc.id, doc.to_dict()))

我尝试通过尝试替换variable1来比较其值并返回文档id来执行以下操作。输出不返回任何响应。

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate('/root/key.json')
firebase_admin.initialize_app(cred)

db = firestore.client()

variable1 = 9129392

docs = db.collection(u'company').where(u'companyid', u'==', variable1).get()

for doc in docs:
    print(u'{} => {}'.format(doc.id, doc.to_dict()))

有人可以告诉我如何解决这个问题吗?谢谢

最佳答案

在 Firestore 中,检查 companyid 是字符串还是长整型(数字)。

大多数人将其存储为字符串 - 如果是这种情况,则需要将变量 1 声明为字符串并在其间添加引号:

variable1 = "9129392"

如果您希望它是整数,请确保它在 Firestore 中存储为数字类型。

关于python - 无法在 Firebase 查询数据中传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53778777/

相关文章:

python - 从字典中随机删除项目

python - 无法在 Ubuntu 中使用 pyglet 编译 python

python - 构建 DeBruijn 图的算法给出了错误的结果

javascript - 从输入中获取一个值以在 Controller Angular JS 中使用

javascript - FirestoreDataConverter.fromFirestore 的签名/类型是什么?

firebase - 如何根据 Firestore 数据库中的字段从集合中搜索文档?

angularfire2 - 如何以非实时方式使用 Cloud Firestore?

python - 两个不同的 tk Canvas 中的两个 matplotlib 图形

firebase - 在每个屏幕上获取当前Firebase用户的正确方法是什么?

javascript - FireStore 交易 : Transaction is not being atomic