python - 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”
解决方案
方法名称是document()
,而不是 ``doc()。您在初始化时正确使用它doc_ref
,但create_counter()
方法不正确。
Hiranya Jayathilaka 的评论副本,因为它是正确答案。OP,您可以接受此评论以结束问题。
推荐阅读
- python - 如何连接到 Jupyter Notebook 中的 USB 端口?
- python-3.x - Pandas 根据匹配复制值
- javascript - 是否可以自定义 JS 路径以执行单击元素(复选框) SELENIUM、Java
- python - 使用 zip 在 for 循环中追加 Numpy
- zsh - 当前命令中的 ZSH 反向查找
- javascript - 剑道js jpg到pdf问题
- laravel - 在 Laravel 7 上使用 axios 发布请求
- flutter - 颤振在已知位置的轮播中添加图像
- java - Docker 中的简单 Java 应用程序 - 无法找到或加载主类
- ios - 如何使用本机模块在 iphone 的内部存储中获取所有 pdf 文件以响应本机