Firestore:聆听子集合中的文档

时间:2019-01-28 17:29:51

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

我正在尝试使用Firestore Documentation中的示例来聆听Python代码中的文档。我在侦听根集合时收到正确的数据,但是在侦听子集合时却什么也没有。 这是我的代码:

db = firestore.client()

# Create a callback on_snapshot function to capture changes
def on_snapshot(col_snapshot, changes, read_time):
    print(col_snapshot, type(col_snapshot))
    print(changes, type(col_snapshot))

root_collection = u'shared-streams'
subcollection = u'shared-streams/eFC4T~lLyT/messages'


# Watch the root collection query (1)
col_query = db.collection(root_collection)
query_watch = col_query.on_snapshot(on_snapshot)

# Watch the subcollection query (2)
col_query = db.collection(subcollection)
query_watch = col_query.on_snapshot(on_snapshot)

子集合存在于Firestore中且为非空。但是在第一种情况下(1),我得到了元素和更改(和更新)的非空列表,在其他情况下(2),只有两个空列表(在update子集合中什么也没有)。据我了解,根/子集合没有区别,所以请解释我在哪里错了。

UPD: node.js中的类似代码可以正常工作,因此看起来像是python客户端库中的错误。

node.js片段:

var db = admin.firestore();

var query = db.collection('shared-streams/eFC4T~lLyT/messages')

var observer = query.onSnapshot(querySnapshot => {
  console.log(`Received query snapshot of size ${querySnapshot.size}`);
  // ...
}, err => {
  console.log(`Encountered error: ${err}`);
});

1 个答案:

答案 0 :(得分:0)

看看my tutorial

my_ref = database.collection("test").limit(2)

# show the values from database
try:
    docs = my_ref.stream()
    for doc in docs:
         print(u'Doc Data:{}'.format(doc.to_dict()))
except:
    print(u'Missing data')
相关问题