未能将数据发送到 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)


牛魔王的故事
浏览 163回答 2
2回答

有只小跳蛙

您需要在文件顶部初始化 firebase 应用程序像这段代码的东西admin.initializeApp(Object.assign({}, functions.config().firebase, {    credential: admin.credential.cert(serviceAccount),}));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript