Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permissions Error with Cloud Function Adding Data to Firestore

I'm trying to setup a Cloud Function that, when ran by Cloud Scheduler, will insert certain data into my Firestore Database. I am doing this in Node.js using the Inline editor provided when creating my Cloud Function.

I keep getting the error:

"Error: function crashed. Details: 7 PERMISSION_DENIED: Missing or insufficient permissions."

On my Firebase dashboard, the logs show my function and the error that I get when I test my Cloud Function, so I'm assuming my function is hitting the database, just not adding the dummy data I was testing with.

index.js:

const Firestore = require('@google-cloud/firestore');
const PROJECTID = 'MY_PROJECT_ID';
const firestore = new Firestore({
  projectId: PROJECTID,
  timestampsInSnapshots: true,
});

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {
    return firestore.collection("users").add({
    first: "Ada",
    last: "Lovelace",
    born: 1815
    });
};

Package.json:

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "@google-cloud/firestore": "0.17.0",
    "semver": "^5.5.1"
  }
}

I also have my rules set for my database as:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
        allow read, write;
    }
  }
}
like image 681
jazzy1331 Avatar asked Apr 15 '26 20:04

jazzy1331


2 Answers

Thanks to @andresmijares I was able to fix my problem. I looked more into the quickstart and changed my index.js as follows (specifically everything before the helloWorld function).

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

admin.initializeApp({
  credential: admin.credential.applicationDefault()
});

const db = admin.firestore();

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
exports.helloWorld = (req, res) => {
/*  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
  */
    return db.collection("users").add({
    first: "Ada",
    last: "Lovelace",
    born: 1815
    });
};

And I got the error

"Code in file index.js can't be loaded. Did you list all required modules in the package.json dependencies? Detailed stack trace: Error: Cannot find module 'firebase-admin'"

Which I was able to fix by adding the 'firebase-admin' dependency into my package.json, as follows:

{
  "name": "sample-http",
  "version": "0.0.1",
    "dependencies": {
    "semver": "^5.5.1",
    "@google-cloud/firestore": "^1.3.0",
    "firebase-admin": "^7.1.1"
  }
}

This was also all done in the inline editor provided when creating my Cloud Function, so no installation of anything was needed.

like image 140
jazzy1331 Avatar answered Apr 18 '26 13:04

jazzy1331


you need to download the sdk key, this is a json file that you can export from your firebase console Project Overview -> Project Settings -> Services Accounts

Then you can instantiate it like this:

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

When using the firebase admin sdk, the firestore security rules do not apply (they are only for client-side operations)

like image 35
andresmijares Avatar answered Apr 18 '26 12:04

andresmijares