Cloud Firestore 模拟器不是 运行,因此调用 Firestore 会影响生产

The Cloud Firestore emulator is not running, so calls to Firestore will affect production

我正在学习 firebase 以与我的 React 网络应用程序集成。我试图为 GET 和 POST 请求编写函数,并 运行 使用我在 vscode 的终端中获得的端点将它们连接到邮递员上。 现在我的代码与 firebase deploy 完美配合,但在我尝试使用 firebase serve 时出现错误。我也不能简单地使用这些命令,我​​必须使用 sudo 运行 它们,然后输入我的密码,然后我才能 运行 它们。 下面是错误列表

The Cloud Firestore emulator is not running, so calls to Firestore will affect production.

⚠ External network resource requested! - URL: "http://169.254.169.254/computeMetadata/v1/instance" - Be careful, this may be a production service.

⚠ External network resource requested! - URL: "http://metadata.google.internal./computeMetadata/v1/instance" - Be careful, this may be a production service.

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.

 at GoogleAuth.getApplicationDefaultAsync (/home/nikunj/socialape-functions/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)

 at processTicksAndRejections (internal/process/task_queues.js:97:5)

 at async GoogleAuth.getClient (/home/nikunj/socialape-functions/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)

 at async GrpcClient._getCredentials (/home/nikunj/socialape-functions/functions/node_modules/google-gax/build/src/grpc.js:92:24)

 at async GrpcClient.createStub (/home/nikunj/socialape-functions/functions/node_modules/google-gax/build/src/grpc.js:213:23)

Caused by: Error

 at WriteBatch.commit (/home/nikunj/socialape-functions/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:415:23)

 at DocumentReference.create (/home/nikunj/socialape-functions/functions/node_modules/@google-cloud/firestore/build/src/reference.js:283:14)
 at CollectionReference.add (/home/nikunj/socialape-functions/functions/node_modules/@google-cloud/firestore/build/src/reference.js:2011:28)
 at /home/nikunj/socialape-functions/functions/index.js:32:10
 at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:559:20
 at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:534:19
 at Generator.next (<anonymous>)
 at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
 at new Promise (<anonymous>)
 at __awaiter (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:4:12)

i functions: Finished "createScream" in ~1s

我实际上通过在 firebase 项目设置中使用服务帐户提供凭据解决了这个问题。我将初始化应用更改为

    var serviceAccount = require("path/to/serviceAccountKey.json");

    admin.initializeApp({
     credential: admin.credential.cert(serviceAccount),
     databaseURL: "https://socialape-6925b.firebaseio.com"
     });```

对我来说,缺少的步骤是我没有使用

初始化 firestore 模拟器
firebase init emulators

是的,我也遇到了同样的错误,但我解决了这个问题。

首先,确保你的项目文件夹中有 .firebaserc 文件 然后使用以下方式初始化管理员:-

admin.initializeApp();