【发布时间】:2021-03-31 09:36:58
【问题描述】:
firestore 数据库的结构如下: 收藏:用户 id 为 user_id 的文档 每个文档都有一个子集合:游戏 每个子集合都有 id 为 game_id 的文档
我的问题是我无法遍历第一个集合 (---> for user in ref_users:) 即使我的 firestore 数据库已经充满了我所说的组织数据。有人可以帮助我了解如何检索这些文档的信息吗?非常感谢。
from flask import Flask, render_template
from google.cloud import firestore
db = firestore.Client()
@app.route('/', methods=['GET'])
def index():
ref_users = db.collection(u'users').get()
games = []
for user in ref_users:
print(f'{user.id} => {user.to_dict()}')
ref_game = db.collection(u'users').document(f'{user.id}').collection(u'games').get()
for rg in ref_game:
print(f'{rg.id} => {rg.to_dict()}')
tmp = rg.to_dict()
tmp['game_id'] = rg.id
tmp['user_id'] = user.id
games.append(tmp)
return render_template('game_list.html', title='Game list', games=games)
【问题讨论】:
-
我不确定我是否理解这个问题。为什么不能遍历
users集合中的文档?
标签: python google-cloud-firestore web-applications rest