首页 > 解决方案 > AttributeError:“CollectionReference”对象没有属性“doc”

问题描述

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import traceback    
import json
cred = credentials.Certificate(constants.path_to_json)
default_app = firebase_admin.initialize_app(cred)
other_app = firebase_admin.initialize_app(cred, name='other')
db = firestore.client()
match_id = 'blala'
pool_id = 'pidxyz'
cont_team_id = '229'

doc_ref = db.collection('match-pools')\
        .document(match_id)\
        .collection('pid')\
        .document(pool_id)\
        .collection('contestant-teams')\
        .document(cont_team_id)

def create_counter(ref, num_shards):
    batch = db.batch()
    print(batch)
    # Initialize the counter document
    batch.set(ref, { 'num_shards': num_shards })
    # Initialize each shard with count=0
    for i in range(0, num_shards):
        shardRef = ref.collection('shards').document(str(i))
        batch.set(shardRef, { count: 0 })
    # Commit the write batch
    return batch.commit()

create_counter(doc_ref, 5)

AttributeError:“CollectionReference”对象没有属性“doc”

标签: pythongoogle-cloud-firestorefirebase-admin

解决方案


方法名称是document(),而不是 ``doc()。您在初始化时正确使用它doc_ref,但create_counter()方法不正确。

Hiranya Jayathilaka 的评论副本,因为它是正确答案。OP,您可以接受此评论以结束问题。


推荐阅读