0
votes

I'm trying to push some random data to cloud firestore. So, I have initialized the project using cloud function with - firebase init functions. Inside the functions dir, I have a file seed.js which has the code to send the data. Using faker to generate the data.

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

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

2 Answers

2
votes

I'm assuming you're following the example from fireship.io

If you look at the example you also have to initialise a firebase configuration first

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

1
votes

You need to initialize the firebase app at the top of the file

Something like this piece of code

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