【问题标题】:Failed to send data to Cloud Firestore无法向 Cloud Firestore 发送数据
【发布时间】:2019-08-30 15:19:53
【问题描述】:

我正在尝试将一些随机数据推送到 Cloud Firestore。所以,我已经使用带有-firebase init 函数的云函数初始化了项目。 在函数目录中,我有一个文件 seed.js,其中包含发送数据的代码。使用 faker 生成数据。

const faker = require("faker");

const db = admin.firestore();

const fakeIt = () => {
  return db.collection("customers").add({
    username: faker.internet.userName(),
    avatar: faker.internet.avatar(),
    bio: faker.hacker.phrase()
  });
};

Array(20)
  .fill(0)
  .forEach(fakeIt);

当我运行 node seed.js 时,我收到了下面描述的错误

PS C:\Users\Ghost\Random Projects\Algolia\functions> node .\seed.js
(node:1636) UnhandledPromiseRejectionWarning: Error: Unable to detect a Project Id in the current environment.
To learn more about authentication and Google APIs, visit:
https://cloud.google.com/docs/authentication/getting-started
    at _getDefaultProjectIdPromise.Promise (C:\Users\Ghost\Random Projects\Algolia\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:90:31)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:1636) 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: 21)
(node:1636) [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.
(node:1636) UnhandledPromiseRejectionWarning: Error: Unable to detect a Project Id in the current environment.
To learn more about authentication and Google APIs, visit:
https://cloud.google.com/docs/authentication/getting-started
    at _getDefaultProjectIdPromise.Promise (C:\Users\Ghost\Random Projects\Algolia\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:90:31)
    at process._tickCallback (internal/process/next_tick.js:68:7)

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore


    【解决方案1】:

    我假设您正在遵循 fireship.io 中的示例

    如果您查看示例,您还必须先初始化 Firebase 配置

    const admin = require('firebase-admin');
    admin.initializeApp();
    

    如果这已经部署到云功能,配置将是automatically provided。如果没有,您将需要提供您的 firesbase 凭据

    【讨论】:

    • 正确。明白了,谢谢
    【解决方案2】:

    您需要在文件顶部初始化 firebase 应用

    类似这段代码

    admin.initializeApp(Object.assign({}, functions.config().firebase, {
        credential: admin.credential.cert(serviceAccount),
    }));
    
    

    【讨论】:

    • 不,根据文档,使用云功能时不需要此配置
    • 来自 Docs-SDK 也可以不带参数初始化。在这种情况下,SDK 使用 Google 应用程序默认凭据并从 FIREBASE_CONFIG 环境变量中读取选项。如果 FIREBASE_CONFIG 变量的内容以 { 开头,它将被解析为 JSON 对象。否则,SDK 会假定该字符串是包含选项的 JSON 文件的名称。
    • FIREBASE_CONFIG 环境变量自动包含在 Cloud Functions 中,用于通过 Firebase CLI 部署的 Firebase 函数。
    • 你设置了这些变量吗?
    • 感谢它的工作。但是有一个警告——警告,FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量丢失。初始化 firebase-admin 将失败。指的是什么?
    猜你喜欢
    • 1970-01-01
    • 2018-11-15
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2022-06-24
    • 1970-01-01
    • 2019-10-29
    相关资源
    最近更新 更多