Firebase聊天应用程序setValue失败,公共数据库出现错误? [英] Firebase chat app setValue failed error with a public database?

查看:94
本文介绍了Firebase聊天应用程序setValue失败,公共数据库出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Firebase的聊天应用,该应用一直保持着

I have a chat app using Firebase that keeps on having a

x处的setValue失败:DatabaseError:权限被拒绝

setValue at x failed: DatabaseError: permission denied

每次输入消息时

错误.

error every time I type a message.

我已将数据库设置为公开:

I set my Database to be public already:

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

我的聊天参考资料中有东西吗?

Is it something from within my chat reference?

private void displayChat() {

    ListView listOfMessage = findViewById(R.id.list_of_message);

    Query query = FirebaseDatabase.getInstance().getReference();
    FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
            .setLayout(R.layout.list_item)
            .setQuery(query, Chat.class)
            .build();

    adapter = new FirebaseListAdapter<Chat>(options) {
        @Override
        protected void populateView(View v, Chat model, int position) {
            //Get reference to the views of list_item.xml
            TextView messageText, messageUser, messageTime;
            messageText = v.findViewById(R.id.message_text);
            messageUser = v.findViewById(R.id.message_user);
            messageTime = v.findViewById(R.id.message_time);

            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
        }
    };
    listOfMessage.setAdapter(adapter);
}

推荐答案

您的代码正在使用Firebase实时数据库,但是您正在更改Cloud Firestore的安全规则.虽然这两个数据库都是Firebase的一部分,但它们完全不同,并且其中一个的服务器端安全规则不适用于另一个.

Your code is using the Firebase Realtime Database, but you're changing the security rules for Cloud Firestore. While both databases are part of Firebase, they are completely different and the server-side security rules for one, don't apply to the other.

当您在Firebase控制台中进入数据库"面板时,您很有可能最终进入 Cloud Firestore规则:

When you go the database panel in the Firebase console, you most likely end up in the Cloud Firestore rules:

如果您使用的是 Cloud Firestore规则, Firebase控制台,您可以通过单击顶部的 Cloud Firestore BETA ,然后从列表中选择实时数据库,更改为实时数据库规则.

If you are on the Cloud Firestore rules in the Firebase console, you can change to the Realtime Database rules by clicking Cloud Firestore BETA at the top, and then selecting Realtime Database from the list.

您还可以通过单击此链接.

与您所拥有的数据库匹配的实时数据库的安全规则是:

The security rules for the realtime database that match what you have are:

{
  "rules": {
    ".read": "auth.uid !== null",
    ".write": "auth.uid !== null"
  }
}

这将授予所有经过身份验证的用户对整个数据库的完全读取和写入访问权限.请阅读我对该问题的答案,以获取更多有关此类规则的安全性/风险权衡的信息:

This will grant any authenticated user full read and write access to the entire database. Read my answer to this question on more on the security/risk trade-off for such rules: Firebase email saying my realtime database has insecure rules.

这篇关于Firebase聊天应用程序setValue失败,公共数据库出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆