6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cloud Functions (Python3.7)からFiresotreへ書き込む

Posted at

Firestoreへの書き込みサンプルが英語の公式ドキュメントに見当たらなかったので
firebase-adminをimportしたら書き込めた

まずrequirements.txtに下記を記述する

firebase-admin==2.13.0

クレデンシャル無しでアプリを初期化して書き込む


import firebase_admin
from firebase_admin import firestore

# 初期化済みのアプリが存在しないか確認する。※複数アプリの初期化はエラーです。的な例外に遭遇したので入れたif文
if len(firebase_admin._apps) == 0:
    # アプリを初期化する
    default_app = firebase_admin.initialize_app()
db = firestore.client()

def hello_world(request):
    # firestoreに書き込み
    db.collection('A').document('docid').set({"foo": "bar"})
    # ブラウザに見せるために返す
    return f'Hello World!'

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?