【问题标题】:why my flask app didnt connect to the cloud firestore? I wrote this code为什么我的烧瓶应用程序没有连接到云 Firestore?我写了这段代码
【发布时间】:2021-03-01 21:00:27
【问题描述】:

当我运行脚本将数据库连接到 cloudfirestore 时,它​​不会引发错误,但不会创建我指定的任何集合。你能帮我修复我的代码逻辑错误吗?谢谢。

def get_db():
"""
This is my database engine to cloud firestore
"""

if 'db' not in g:
    cred = credentials.Certificate("key.json")
    firebase_admin.initialize_app(cred)
    g.db = firestore.client()


return g.db

def init_db():
   """
   Function that creates all collections
   """
   db = get_db()

   for collection in collectionsName:
       db.collection(collection)


@click.command("init-db")
@with_appcontext
def init_db_command():
   """
   I initialize db with command 'init-db'
   """
   init_db()
   click.echo("base de datos inicializada")

def init_app(app):
   """
   run my flask app and connect to my cloud firestore db
   """

   app.cli.add_command(init_db_command)

这就是我运行脚本的方式:flask init-db

我的烧瓶 init.py 看起来像这样

  def create_app():
      app = Flask(__name__)

      from . import db
      db.init_app(app)


      return app

【问题讨论】:

  • 是什么让您认为它没有连接?它抛出错误了吗?它是什么,发生在哪条线上?
  • 在问题的正文中描述问题,而不是在问题的标题中。
  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图,不是链接到外部门户)有问题(不是评论)。还有其他有用的信息。
  • 请通过发布您收到的错误和您遵循的文档来提供有关您所面临问题的更多信息
  • 它不会引发任何错误,但是当我尝试在 Cloud Firestore 中创建集合时,没有出现任何内容

标签: python firebase flask google-cloud-firestore


【解决方案1】:

要初始化集合,请在集合中至少创建一个文档。例如:

def init_db():
   """
   Function that creates all collections
   """
   db = get_db()
   # for each collection, create an initial doc
   for collection in collectionsName:
       db.collection(collection).document('init_coll').set({'exists': True})

【讨论】:

    猜你喜欢
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多