Firebase Cloud Functions Firestore Trigger produces: Error: 7 PERMISSION_DENIED: Missing or insufficient permissions

后端 未结 6 594
囚心锁ツ
囚心锁ツ 2020-12-29 13:48

I\'m trying to use a Firebase Cloud Function to update a document within my Firestore database, when one of my documents has been updated using a trigger. The trigger is cal

6条回答
  •  时光取名叫无心
    2020-12-29 13:58

    One solution that worked for me:

    • I was switching between firebase projects where I wanted to emulate a Cloud Function and see the results in production in Firestore (I already had the Cloud Function creating data in production Firestore in a project (ex. "project-dev")
    • Permission denied error kept happening when creating new document in Firestore via the cloud function even though I was using the downloaded the serviceAccount credentials for the new project ("project-sandbox")
    let serviceAccount = require('../credentials-sb.json');
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "https://-sandbox.firebaseio.com"
    });
    
    • I noticed I had not yet run the firebase use command for my new project, nor was it added to .firebaserc. I was "using" a project other than the one that the credentials file would have needed (ex: Firebase CLI set to use "project-dev", I had not yet run the firebase use command for my new "project-sandbox")
    • After I ran firebase use project-sandbox, I ran npm run shell and executed my function from the emulator and everything worked. The emulator worked as it should for "project-sandbox" as it had for "project-dev".

    I can only assume this is due to setting a project-id environment variable which is either sensed from the environment when running admin.initializeApp() or that I could have set manually like this (see comment from @Seb above):

    let serviceAccount = require('../credentials-sb.json');
    admin.initializeApp({
        project-id: '-sandbox'
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "https://inkling-sandbox.firebaseio.com"
    });
    

    I hope this helps! Please let me know if it did.

提交回复
热议问题