错误7:缺少或权限不足 cloud functions/firestore。

11 浏览
0 Comments

错误7:缺少或权限不足 cloud functions/firestore。

我正在使用 Firebase 作为后端编写应用程序,试图写入数据库,但是出现了“错误 7:权限不足或缺失”的错误。有人告诉我 Cloud Functions 可以绕过 Firebase 权限控制,所以我不确定造成这个错误的原因是什么。请有人帮我看一下吗?

这是我的权限:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

这是我的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
var express = require('express');
admin.initializeApp(functions.config().firebase);
const app = express();
let db = admin.firestore();
app.get('/helloworld', (req, res) => res.send('Hello World!'));
app.post('/signup', (req, res) => {
  var email = req.body.email;
  var username = req.body.username;
  var password = req.body.password;
  //creating document.  Here is where it isn't working
  let docRef = db.collection('UsersMain').doc('firstdoc');
    let data = {
    Email: 'a@gmail.com',
    UserName: 'Matt'
  };
  let setDoc = docRef.set(data).then(() =>  {
    res.send('Login Complete');
  })
  .catch(function(error) {
    console.error("Error adding document: ", error);
});
});
const api1 = functions.https.onRequest(app);
module.exports = {api1};

我收到的错误消息如下:

{ Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
>      at Object.callErrorFromStatus (/Users/mg8686./Desktop/mattsapp/mattsappfinal/backend/functions/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
>      at Http2CallStream.call.on (/Users/mg8686./Desktop/mattsapp/mattsappfinal/backend/functions/node_modules/@grpc/grpc-js/build/src/client.js:96:33)
>      at Http2CallStream.emit (events.js:203:15)
>      at process.nextTick (/Users/mg8686./Desktop/mattsapp/mattsappfinal/backend/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:97:22)
>      at process._tickCallback (internal/process/next_tick.js:61:11)
>    code: 7,
>    details: 'Missing or insufficient permissions.',
>    metadata: Metadata { internalRepr: Map {}, options: {} } }

请有人帮我看一下吗?谢谢!

admin 更改状态以发布 2023年5月20日
0
0 Comments

@Matias Seguel的解决方案对我有效,可以在这里找到:Firebase Cloud Functions Firestore Trigger produces: Error: 7 PERMISSION_DENIED: Missing or insufficient permissions

虽然不是最佳的生产环境解决方案,但是对于开发目的可以使用。

0