javascript - 无法将数据发送到 Cloud Firestore

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

我正在尝试将一些随机数据推送到云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)

最佳答案

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

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

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

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

关于javascript - 无法将数据发送到 Cloud Firestore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57729415/

相关文章:

javascript - Accordion :自动滚动到 Pane 内容的顶部

javascript - 基于 Angular Reactive Forms 验证更改输入元素 CSS

javascript - Google Maps APIv3 - 通过地理编码添加标记

javascript - ESM 树摇动/死代码消除如何工作?

Android通知有一个彩色图标而不是变成白色

javascript - Ext Js 6.2,如何在西面板触发时自动展开东面板?

javascript - Expressjs/node.js 相当于 php 中的 exit()

javascript - 在多个表中同时获取结果

swift - 图片不使用 Firebase url 缓存,但使用其他缓存

android - 保存数据时登录失败。(注册)