要するに:
admin.initializeApp({ credential: admin.credential.applicationDefault() });
admin.credential.applicationDefault()のドキュメントを参照してください
更新:これはテスト/実験にのみ推奨されることに注意してください:
この戦略は、テストや実験の際に役立ちますが、アプリケーションが使用している資格情報を特定するのが難しくなる可能性があります。アプリケーションが使用する資格情報を明示的に指定することをお勧めします...ソース
もう少し情報
バッチでfirestoreデータベースのいくつかのドキュメントを更新しようとするfirebase関数をローカルで呼び出そうとしたときにも同じことがありました。(バッチなしでテストしませんでした)。
ローカルで firebase 関数の呼び出しを開始するには、次を使用します。
firebase function:shell
ご存じのとおり、これはプロジェクトで使用可能な関数を一覧表示します。
関数を呼び出したところ、次のエラー コールスタックが表示されました。
Unhandled error Error: The incoming JSON object does not contain a client_email field
> at JWT.fromJSON (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\jwtclient.js:165:19)
> at GoogleAuth.fromJSON (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\googleauth.js:294:16)
> at GoogleAuth.getClient (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\googleauth.js:476:52)
> at GrpcClient._getCredentials (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-gax\build\src\grpc.js:107:40)
> at GrpcClient.createStub (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-gax\build\src\grpc.js:223:34)
> at new FirestoreClient (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\v1\firestore_client.js:128:39)
> at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\index.js:315:26)
> at ClientPool.acquire (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\pool.js:61:35)
> at ClientPool.run (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\pool.js:114:29)
> at Firestore.readStream (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\index.js:995:26)
RESPONSE RECEIVED FROM FUNCTION: 500, {
"error": {
"status": "INTERNAL",
"message": "INTERNAL"
}
}
コマンドラインを使用してローカルで関数を実行していました。
firebase functions:shell
私はこのコードを使用していました:
// Reference report in Firestore
const db = admin.firestore();
admin.initializeApp();
export const performMyCallableFirebaseFunction = (db, { from, to }) => {
return db.collection("collectionName").where("prop", "==", from).limit(500).get().then(snapshot => {
if (snapshot.empty) return new Promise(resolve => resolve(`No docs found with prop: ${from}`));
const batch = db.batch();
snapshot.forEach(doc => batch.update(doc.ref, { prop: to }));
return batch.commit();
});
};
exports.myCallableFirebaseFunction = functions.https.onCall(data => performMyCallableFirebaseFunction(db, data.from, data.to));
ラインを変えました
admin.initializeApp();
に
admin.initializeApp({ credential: admin.credential.applicationDefault() });
そして今、私は自分の関数をローカルで呼び出すことができました:
firebase functions:shell
firebase > myCallableFirebaseFunction({from: "foo", to: "bar"})
admin.credential.applicationDefault()のドキュメントを参照してください