簡體   English   中英

無法將數據發送到Cloud Firestore

[英]Failed to send data to Cloud Firestore

我正在嘗試將一些隨機數據推送到Cloud Firestore。 因此,我已使用帶有-firebase init函數的雲函數初始化了該項目。 在函數dir中,我有一個seed.js文件,其中包含發送數據的代碼。 使用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);

當我運行節點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();

如果已將其部署到雲功能,則將自動提供配置 如果不是,則需要提供您的firesbase憑據

您需要在文件頂部初始化firebase應用

類似於這段代碼

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