0

I'm following along a tutorial where I'm creating google cloud functions to add dummy data to google Firestore. When I test locally using Firebase Serve, I get an error "Could not load the default credentials" after sending a POST request to the localhost endpoint. However, when I test on production using Firebase Deploy, I get no error after sending a POST request to the production endpoint and the database entry is created successfully.

const functions = require('firebase-functions');
const admin = require('firebase-admin'); 

admin.initializeApp(functions.config().firebase);
let db = admin.firestore();

exports.createScream = functions.https.onRequest((req, res) => {
    let scream = {
        body: req.body.body, 
        userHandle: req.body.userHandle, 
        createdAt: admin.firestore.Timestamp.fromDate(new Date())
    }; 

    let addScream = db.collection('screams').add(scream)
        .then(ref => {
        res.json({message: `Successfully created scream at ${ref.id}`})})
        .catch(err => {res.status(500).json({error: `${err}`})});
});
pizza7
  • 441
  • 4
  • 17
  • 1
    This might be helpful: https://stackoverflow.com/questions/42043611/could-not-load-the-default-credentials-node-js-google-compute-engine-tutorial – Casper Sep 06 '19 at 04:48

1 Answers1

0

For you to access your firebase project you need to have credentials to your project. It works on the cloud because this configuration is already set up for you.

If you go to your firebase console project, you should go to settings => service accounts where you can download a json file with your credentials.

enter image description here

Now create an environment variable, that points to the file you just downloaded.

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

This is in the official documentation

ajorquera
  • 1,297
  • 22
  • 29