如何在一次交易中在Firebase上创建用户和上传数据 [英] How to create user and upload data on Firebase in one single transaction

查看:80
本文介绍了如何在一次交易中在Firebase上创建用户和上传数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Web应用程序,该应用程序使用电子邮件和密码 firebase.auth().createUserUsingEmailAndPassowrd()

I am making a web application which creates a user on Firebase using email and password firebase.auth().createUserUsingEmailAndPassowrd()

创建帐户后,我立即根据Firebase数据库上的表单值上传一些数据.

After the account is created, I immediately upload some data based on the forms values on the firebase database.

一切正常,但有时,在最坏的情况下,虽然创建了用户,但由于网络问题,无法上传数据.该应用程序已经重定向到需要该数据的位置,整个应用程序将失败.

All works well, but sometimes, In worst case scenarios, the user is created but due to network issues the data is not uploaded. And the app is already redirected to a place where it needs that data and the whole app fails.

如何确定只有在我尝试上传到数据库上的数据也被更新时,才创建用户帐户.

How can I make it certain that the user account is created only if the data I am trying to upload on database gets updated too.

摘要:确保在创建用户的同时上传了一些数据,或者根本没有创建用户.

Summary : Make sure that some data is uploaded along with user creation or the user doesn't get created at all.

推荐答案

由于您担心最坏的情况(这是绝对正常的),因此,有一种解决方法:考虑您具有以下字段:电子邮件,密码和地址.地址字段是您需要存储在数据库中的地址字段(当然还有用户uid).

Because you're so afraid of worst case scenario (which is absolutely normal), here is one way to do it: Consider you have these fields: email, password, and address. The address field is the one you need to store in your database (with user uid, of course).

首先,您尝试注册用户,并且像 Faizy said 一样,您使用onCompleteListener这样

First you try to register the user, and like Faizy said, you use onCompleteListener like this

mAuth.createUserWithEmailAndPassword(email, password)
    .onCompleteListener(... {
        ... onComplete (... task) {
            if (task.isSuccessful()) {
                doInsertTheData();
            }
...

任务成功后,您想要将数据插入创建doInsertTheData()的数据库中

When task is successful, then you want to insert the data into database, which doInsertTheData() is created for

private void doInsertTheData() {
     // I believe you familiar with Firebase database inserting method, so I skip the long part
     FirebaseDatabase...setValue(address, new CompletionListener() {
         ... onComplete(FirebaseError firebaseError, ...) {
             if (firebaseError == null) {
                 // if your code reach here, its a happy ending
             } else {
                 // if it reach here, then you can remove the signed in user
                 // and tell the user that their registration is failed and should try again
                 FirebaseAuth.getInstance().getCurrentUser().delete();
             }

然后,只要数据库保存过程失败,用户凭据就会被删除

And there you go, as long as the database saving process failed, the user credential will be deleted

注意:实际上,我在键入此方法时会想到另一种方法,但是它太复杂了,我认为该方法更容易理解(虽然我没有尝试/使用此方法)

Note: I actually have another method in mind when I type this, but it's too complicated and I think this one is more easy to understand (I haven't try/use this though)

这篇关于如何在一次交易中在Firebase上创建用户和上传数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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