「Gcp/Firebase/Firestore/async」の版間の差分
提供: 初心者エンジニアの簡易メモ
| 行1: | 行1: | ||
| − | + | ==通常のfirestore取得をasync/awaitに変更する方法== | |
| + | functionにasyncを追加し、firestoreのget()にawaitを追加すればよい。 | ||
| − | == | + | ==async/awaitを使わないfirestoreのサンプル== |
<pre> | <pre> | ||
const functions = require('firebase-functions'); | const functions = require('firebase-functions'); | ||
| 行22: | 行23: | ||
</pre> | </pre> | ||
| − | == | + | ==async/awaitを使うfirestoreのサンプル== |
<pre> | <pre> | ||
exports.dbasync = functions | exports.dbasync = functions | ||
2019年8月12日 (月) 21:44時点における版
通常のfirestore取得をasync/awaitに変更する方法
functionにasyncを追加し、firestoreのget()にawaitを追加すればよい。
async/awaitを使わないfirestoreのサンプル
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.db = functions
.region('asia-northeast1')
.https.onRequest((request, response) => {
admin.firestore().collection('scores')
.orderBy('maxKanaSpeed', 'desc')
.limit(50)
.get()
.then((snapshot) =>
{
var scores = snapshot.docs.map(x => x.data());
var json = JSON.stringify(scores);
response.send("json" + json);
});
});
async/awaitを使うfirestoreのサンプル
exports.dbasync = functions
.region('asia-northeast1')
.https.onRequest(async(request, response) => {
var snapshot = await admin.firestore().collection('scores')
.orderBy('maxKanaSpeed', 'desc')
.limit(50)
.get();
var scores = snapshot.docs.map(x => x.data());
var json = JSON.stringify(scores);
response.send("json" + json);
});
