简体   繁体   English

无法将数据发送到Cloud Firestore

[英]Failed to send data to Cloud Firestore

I'm trying to push some random data to cloud firestore. 我正在尝试将一些随机数据推送到Cloud Firestore。 So, I have initialized the project using cloud function with - firebase init functions. 因此,我已使用带有-firebase init函数的云函数初始化了该项目。 Inside the functions dir, I have a file seed.js which has the code to send the data. 在函数dir中,我有一个seed.js文件,其中包含发送数据的代码。 Using faker to generate the data. 使用fakerr生成数据。

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);

When I run node seed.js, I'm getting the error described below 当我运行节点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)

I'm assuming you're following the example from fireship.io 我假设您正在遵循fireship.io中的示例

If you look at the example you also have to initialise a firebase configuration first 如果您查看示例,则还必须首先初始化Firebase配置

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

If this has already been deployed to cloud functions the config will be automatically provided . 如果已将其部署到云功能,则将自动提供配置 If not you will need to provide your firesbase credentials 如果不是,则需要提供您的firesbase凭据

You need to initialize the firebase app at the top of the file 您需要在文件顶部初始化firebase应用

Something like this piece of code 类似于这段代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM