本文介绍了通过功能将Firebase错误写入Firestore:"7 PERMISSION_DENIED:权限缺失或不足"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的Firebase函数,该函数使用TypeScript将记录添加到Firestore.该代码的简化版本为:

I'm trying to write a simple Firebase Function that adds a record to Firestore with TypeScript. The cut down version of the code is:

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";

const config = functions.config();

admin.initializeApp(functions.config().firebase);
admin.database.enableLogging(true);

exports.subscriptions = functions.https.onRequest(
  async (req, res) => {
    try {
      const requestSubscription = req.body;
      const subscription = {
        subscription_id: requestSubscription.subscription_id,
        email: requestSubscription.email || ""
      };

      const id = subscription.subscription_id;
      const dbSubscriptions = admin.firestore().collection("subscriptions");
      await dbSubscriptions.doc(id).set(subscription);

      return res.status(200).send(`Success`);
    } catch (e) {
      console.error(e.stack);
      return res.status(500).send(`Error`);
    }
  }
);

这在本地可以正常工作.部署时,出现此错误:

This works as expected locally. When I deploy, I get this error:

Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
    at Object.exports.createStatusError (/user_code/node_modules/firebase-admin/node_modules/grpc/src/common.js:87:15)
    at Object.onReceiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:1214:28)
    at InterceptingListener._callNext (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:590:42)
    at InterceptingListener.onReceiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:640:8)
    at callback (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:867:24)

我的functions/package.json文件是:

My functions/package.json file is:

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    "express": "4.16.3",
    "firebase-admin": "5.12.1",
    "firebase-functions": "1.0.3"
  },
  "devDependencies": {
    "tslint": "^5.8.0",
    "typescript": "^2.5.3"
  },
  "private": true
}

我正在搜索此错误的唯一链接是以下未回答的问题: PERMISSION_DENIED Firestore CloudFunction TypeScript

The only link I'm getting searching for this error is this unanswered question:PERMISSION_DENIED Firestore CloudFunction TypeScript

我已将Firestore规则设置为允许任何人读写,即使以上内容使用的是admin,但仍然无法正常工作.

I've set my Firestore rules to allow anyone to read and write as well even though the above is using admin and it still doesn't work.

任何人都可以帮忙吗?

推荐答案

我就此问题联系了Firebase支持,他们也不知道发生了什么.我创建了一个新项目,进行了部署并成功运行.也许这只是新项目的一个怪异故障.

I contacted Firebase support about this and they didn't know what was going on either. I created a new project, deployed and it worked. Perhaps it's just a weird glitch with new projects.

这篇关于通过功能将Firebase错误写入Firestore:"7 PERMISSION_DENIED:权限缺失或不足"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 12:15