简体   繁体   English

Firebase setDoc 导致 async/await 错误

[英]Firebase setDoc results in a async/await error

Right now, I am making an app for my client and I have a form that should register a new user.现在,我正在为我的客户制作一个应用程序,并且我有一个应该注册新用户的表单。 I have initialized my firebase app, both in project and in the console.我已经在项目和控制台中初始化了我的 firebase 应用程序。 I put setDoc operation in my submit function(and I used async/await), but in the console I get the following error:我将 setDoc 操作放在我的提交函数中(并且我使用了 async/await),但在控制台中我收到以下错误:

Uncaught (in promise) FirebaseError: Missing or insufficient permissions.

I am making my project using react, if that helps.如果有帮助,我正在使用 React 制作我的项目。 Here is my function:这是我的 function:

const submit = async () => {
        if(name.length >= 3 && surname.length >= 3 && phone.length >= 9 && code.length == 3)
        {
            const userName = name + surname
            const ref = doc(db, 'users', userName)
            const data = {
                name: name,
                surname: surname,
                phone: phone,
                code: code,
                userName: name + surname
            }
            console.log(data)
            await setDoc(ref, data).then((res) => {
                window.alert('Регистрацијата е успешна!')
                router('/')
            })
        }
        if(name.length <= 2) setNameError(true)
        if(surname.length <= 2) setSurnameError(true)
        if(phone.length <= 8) setPhoneError(true)
        if(code.length != 3) setCodeError(true)
        
    }

Thanks in advance to the people that will answer this question:)提前感谢将回答这个问题的人:)

The FirebaseError: Missing or insufficient permissions. FirebaseError: Missing or insufficient permissions. error means that you have to adapt your security rules to allow writing in the users collection.错误意味着您必须调整安全规则以允许写入users集合。

The most basic possible rule would be:最基本的可能规则是:

service cloud.firestore {
  match /databases/{database}/documents {

    // Match any document in the 'users' collection
    match /users/{userId} {
      allow read: ...;
      allow write: if true;
    }
  }
}

But it would allow everyone to write to the collection, so it's not really recommended.但它会允许每个人都写到集合中,所以不是很推荐。 However it may be interesting to temporarily use it on a test environment to check that your code is correct.但是,在测试环境中临时使用它来检查您的代码是否正确可能会很有趣。

Read the doc on security rules in order to find the exact rule corresponding to your needs.阅读有关安全规则的文档,以找到与您的需求相对应的确切规则。

Note that you will probably be interested by the common pattern shown in the doc which aims at "making sure users can only read and write their own data".请注意,您可能会对文档中显示的通用模式感兴趣,该模式旨在“确保用户只能读取和写入自己的数据”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM