无法获取客户端数据以显示[firestore] [英] Unable to get client data to display [firestore]

查看:71
本文介绍了无法获取客户端数据以显示[firestore]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库设置为/clients,并使用firebase auth来处理用户.我想这样做,以便用户只能看到,编辑和删除他们创建的客户端数据.当前的Firestore安全规则为.

My database is setup as /clients and uses firebase auth to handle users. I want to make it so users can only see, edit, and delete client data they created. The current firestore security rule is.

[code]service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /clients/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}[/code]

使用此规则,用户可以将新客户端添加到数据库中,但是不会显示网站上的客户端.我有代码设置,以便当用户添加客户端时,它将用户UID附加到"userId"下的客户端.显示客户端的代码是

With this rule users are able to add new clients into the database, however the clients on the website are not showing. I have the code setup so that when a user adds a client it attaches the users UID to the client under 'userId'. The code to display the clients is

[code]<tbody>
                    {clients.map(client => (
                        <tr key={client.id}>
                            <td>{client.firstName} {client.lastName}</td>
                            <td>{client.dateCreated}</td>
                            <td><a href={`tel:${client.phone}`}>{client.phone}</a></td>
                            <td>
                                <Link to={`/client/${client.id}`} className="btn btn-secondary btn-sm">
                                    <i className="fas fa-arrow-circle-right"></i> Details
                                </Link>
                            </td>
                        </tr>
                    ))}
                 </tbody>

[/code]

我不确定自己在做什么错,是安全规则还是我选择显示数据的方式?

I'm not sure what I'm doing wrong, is it the security rules or how I'm choosing to display the data?

推荐答案

我能够获取结果,但无法通过安全规则获取.调整了安全规则和数据库变量,以便将用户ID附加到他们添加的数据后,我尝试了@firebaser建议的规则.我能够按照上述规则创建客户端,但是,我无法查看它们,并且在控制台中收到配置文件侦听器错误:缺少权限或权限不足.FirebaseError:缺少权限或权限不足".

I was able to get my result but not via security rules. After adjusting the security rules and my database variables so that users id is attached to the data they add, I attempted the rule suggested by @firebaser. I was able to create clients under that rule as mentioned above however, I couldn't view them and in the console I got "Error with profile listener: Missing or insufficient permissions. FirebaseError: Missing or insufficient permissions".

基于该错误,看来我必须在我的项目文件夹的node模块中编辑一个firebase文件.我不想冒险修改那些文件,所以我没有使用安全规则来过滤数据,而是改变了客户端数据的显示方式.我在.map函数之前添加了.filter,以便它仅显示链接到用户ID的客户端.虽然我的安全规则仅允许已登录的用户读取和写入数据. [代码]

Based on the error it looked like I'd have to edit one of the firebase files in the node module of my project folder. I didn't want to risk modifying those files so instead of using the security rules to filter data, I just altered how the client data gets displayed. I added a .filter before the .map function so that it will only show the clients linked to the users id. While my security rules only allows logged in users to read and write data. [code]

<tbody>
            {clients.filter(client => client.userId === firebase.auth().currentUser.uid).map(client => (
                <tr key={client.id}>
                    <td>{client.firstName} {client.lastName}</td>
                    <td>{client.dateCreated}</td>
                    <td><a href={`tel:${client.phone}`}>{client.phone}</a></td>
                    <td>
                        <Link to={`/client/${client.id}`} className="btn btn-secondary btn-sm">
                            <i className="fas fa-arrow-circle-right"></i> Details
                        </Link>
                    </td>
                </tr>
            ))}
         </tbody>

[/code]

我真的不能告诉任何人这种方法的安全性,但是它可以工作.

I can't really tell anyone how secure this method would be, but it works.

这篇关于无法获取客户端数据以显示[firestore]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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