【问题标题】:Error 7: Missing or insufficient permissions cloud functions/firestore错误 7:云函数/firestore 缺少或权限不足
【发布时间】:2020-05-20 23:35:15
【问题描述】:

我正在编写一个使用 firebase 作为后端的应用程序,并且我正在尝试写入数据库,但收到“错误 7:权限缺失或不足”。有人告诉我云功能绕过了 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: {} } }


有人可以看看吗?谢谢!

【问题讨论】:

标签: firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

@Matias Seguel 的解决方案对我有用:Firebase Cloud Functions Firestore Trigger produces: Error: 7 PERMISSION_DENIED: Missing or insufficient permissions

这不是一个很好的生产解决方案,但出于开发目的它可以工作。

【讨论】:

    【解决方案2】:

    这似乎是一个常见错误,似乎有多种不同的解决方案,这取决于您目前担心的问题。

    例如,有一个简单的解决方案可以立即为您提供帮助,但不是很推荐,即使用以下配置:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth != null;
        }
      }
    }
    

    因此,我建议您查看以下链接,以验证可能的替代方案以及哪个更适合您。

    如果这些信息对您有帮助,请告诉我!

    【讨论】:

    • 嗨@gso_gabriel。感谢您的帮助!不幸的是,我已经将数据库读/写权限更改为 true,这甚至不起作用。我现在正在寻找另一种解决方案。
    猜你喜欢
    • 2020-09-01
    • 2019-02-26
    • 2018-04-25
    • 2020-09-06
    • 2019-01-21
    • 2022-01-25
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多