使用真实数据库 Firestore 本地 http:Cloud Firestore 模拟器不是 运行,因此数据库操作将失败并出现 'default credentials' 错误
Firestore local http with real db: The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error
我想 运行 一个 firebase http 云函数在本地访问一个真正的远程 firestore 数据库。
为此,我运行在本地使用我的函数:
FIREBASE_CONFIG="/path-to-credential.json" GOOGLE_APPLICATION_CREDENTIALS="/path-to-credential.json" firebase emulators:start --only functions
如此处所写 https://firebase.google.com/docs/admin/setup,这应该允许在没有任何参数的情况下初始化 admin sdk。我的功能index.js
如下
import * as admin from 'firebase-admin';
import * as fromHttpFunc from './http/func';
admin.initializeApp();
export const httpFunc = fromHttpFunc.httpFunc;
当我在浏览器中调用该函数时,这就是我在日志中得到的结果
[warn] ⚠ The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error.
[debug] [2019-07-20T13:16:28.656Z] [runtime-status] Ephemeral server survived. {}
[info] > (node:41939) UnhandledPromiseRejectionWarning: Error: The incoming JSON object does not contain a client_email field
[info] > at JWT.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/jwtclient.js:165:19)
[info] > at GoogleAuth.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:294:16)
[info] > at GoogleAuth.getClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:476:52)
[info] > at GrpcClient._getCredentials (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:107:40)
[info] > at GrpcClient.createStub (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:223:34)
[info] > at new FirestoreClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:128:39)
[info] > at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/index.js:315:26)
[info] > at ClientPool.acquire (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/pool.js:61:35)
[info] > at ClientPool.run (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/pool.js:114:29)
[info] > at Firestore.readStream (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/index.js:995:26)
[info] > (node:41939) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
[info] > (node:41939) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
我不明白哪里出了问题,我还通过执行 cat /path-to-credential.json
检查了凭证的路径是否正常,并且我有一个带有 client_email
字段的 json。
通过确保以下几点,设法让本地云功能与真正的远程数据库一起工作:
运行 firebase login
并登录到您要使用的项目
关注此评论 https://github.com/firebase/firebase-functions/issues/121#issuecomment-337065268 以确保该文件不存在(我有一个 invalid_grant
错误)
运行 export GOOGLE_APPLICATION_CREDENTIALS="SERVICE_ACCOUNT_PATH.json"
在您要使用的终端会话中(替换 SERVICE_ACCOUNT_PATH)
不是通过使用 firebase emulators:start --only functions
而是使用 firebase serve --only functions
来提供您的 http 功能。目前,这是一个未记录的行为:https://github.com/firebase/firebase-tools/issues/1412#issuecomment-504561828.
按如下方式初始化应用程序(更新 YOUR_APP_ID):
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
我想 运行 一个 firebase http 云函数在本地访问一个真正的远程 firestore 数据库。
为此,我运行在本地使用我的函数:
FIREBASE_CONFIG="/path-to-credential.json" GOOGLE_APPLICATION_CREDENTIALS="/path-to-credential.json" firebase emulators:start --only functions
如此处所写 https://firebase.google.com/docs/admin/setup,这应该允许在没有任何参数的情况下初始化 admin sdk。我的功能index.js
如下
import * as admin from 'firebase-admin';
import * as fromHttpFunc from './http/func';
admin.initializeApp();
export const httpFunc = fromHttpFunc.httpFunc;
当我在浏览器中调用该函数时,这就是我在日志中得到的结果
[warn] ⚠ The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error.
[debug] [2019-07-20T13:16:28.656Z] [runtime-status] Ephemeral server survived. {}
[info] > (node:41939) UnhandledPromiseRejectionWarning: Error: The incoming JSON object does not contain a client_email field
[info] > at JWT.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/jwtclient.js:165:19)
[info] > at GoogleAuth.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:294:16)
[info] > at GoogleAuth.getClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:476:52)
[info] > at GrpcClient._getCredentials (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:107:40)
[info] > at GrpcClient.createStub (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:223:34)
[info] > at new FirestoreClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:128:39)
[info] > at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/index.js:315:26)
[info] > at ClientPool.acquire (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/pool.js:61:35)
[info] > at ClientPool.run (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/pool.js:114:29)
[info] > at Firestore.readStream (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/index.js:995:26)
[info] > (node:41939) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
[info] > (node:41939) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
我不明白哪里出了问题,我还通过执行 cat /path-to-credential.json
检查了凭证的路径是否正常,并且我有一个带有 client_email
字段的 json。
通过确保以下几点,设法让本地云功能与真正的远程数据库一起工作:
运行
firebase login
并登录到您要使用的项目关注此评论 https://github.com/firebase/firebase-functions/issues/121#issuecomment-337065268 以确保该文件不存在(我有一个
invalid_grant
错误)运行
export GOOGLE_APPLICATION_CREDENTIALS="SERVICE_ACCOUNT_PATH.json"
在您要使用的终端会话中(替换 SERVICE_ACCOUNT_PATH)不是通过使用
firebase emulators:start --only functions
而是使用firebase serve --only functions
来提供您的 http 功能。目前,这是一个未记录的行为:https://github.com/firebase/firebase-tools/issues/1412#issuecomment-504561828.按如下方式初始化应用程序(更新 YOUR_APP_ID):
admin.initializeApp({ credential: admin.credential.applicationDefault() });