Saim Mahmood
Saim Mahmood

Reputation: 446

Data not entering into firebase

I have an app in which a user has 2 options, Sign in and Sign up. In the sign in option, the user enters his firebase username and password and goes to the MainActivity successfully.

In the Signup option, the user sees 4 screens simultaneously in which he/she is asked to enter some data about themselves. In the last screen, the user is asked for his email/username for firebase registration but for some reason, it gives an error when entering the data into firebase DB. And the most annoying thing is that android studio isn't even showing me the error.

Anyhow, the code is below. I have marked as a comment, "I think the main problem arises here." as a comment in my code. The flow is like this, it comes from RegScreen1 -> RegScreen2 -> RegScreen3 -> RegScreen4 -> MainActivity. But, for some reason, after RegScreen4 -> RegScreen3 appears.

            Bundle b = getIntent().getExtras();

            final String name = b.getString("name");
            final String dob = b.getString("dob");
            final String gender = b.getString("gender");
            final String age = b.getString("age");
            final String smoke = b.getString("smoke");
            final String type = b.getString("type");
            final String today = b.getString("today");
            final String email = emailEditTextRegScreenFour.getText().toString();
            final String password = passwordEditTextRegScreenFour.getText().toString();
            if (email.contains("@") && email.contains(".com"))
            {
            Toast.makeText(RegistrationScreenFour.this, "@ and com available", Toast.LENGTH_SHORT).show();
            Toast.makeText(RegistrationScreenFour.this, "email: " + email + "\npassword: " + password, Toast.LENGTH_SHORT).show();
            firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(RegistrationScreenFour.this, new OnCompleteListener<AuthResult>()
            {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (task.isSuccessful())
                    {
                        SQLiteDatabase db = dbHelper.getWritableDatabase();

                        ContentValues values = new ContentValues();
                        values.put(PersonContract.Person.COL_SMOKER_NAME, name);
                        values.put(PersonContract.Person.COL_SMOKER_DOB, dob);
                        values.put(PersonContract.Person.COL_SMOKER_GENDER, gender);
                        values.put(PersonContract.Person.COL_SMOKER_AGE_STARTED_SMOKING, age);
                        values.put(PersonContract.Person.COL_SMOKER_SMOKES_IN_ONE_DAY, smoke);
                        values.put(PersonContract.Person.COL_SMOKER_SMOKE_ANY_TODAY, today);
                        values.put(PersonContract.Person.COL_SMOKER_USERNAME, email);
                        values.put(PersonContract.Person.COL_SMOKER_PASSWORD, password);
                        values.put(PersonContract.Person.COL_SMOKER_LAST_LOGIN, true);

                        //dbHelper.addSmokerEntry(values);
                        PersonDetails personDetails =
                                new PersonDetails(name, Integer.parseInt(age),
                                        dob, gender, smoke, today, email, password, type);

                        // I think the main problem arises here. 
                        FirebaseDatabase database = FirebaseDatabase.getInstance();
                        DatabaseReference myRef = database.getReference();
                        DatabaseReference courseRef = database.getReference("PersonDetails");
                        courseRef.child(email).setValue(personDetails);

                        Toast.makeText(RegistrationScreenFour.this, "task successful", Toast.LENGTH_SHORT).show();
                        Intent i = new Intent(RegistrationScreenFour.this, MainActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(i);
                    }

Upvotes: 0

Views: 1311

Answers (1)

I found one more mistake

You have to initialize Your firebase database object like this. "PersonDetails" will be a child of Your database.

DatabaseReference courseRef = FirebaseDatabase.getInstance().getReference().child("PersonDetails");
courseRef.child(email).setValue(personDetails);

I face the same problem.

After creating User with E-mail and Password when You are storing data to Fire-base database FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference courseRef = database.getReference().child("PersonDetails"); courseRef.child(email).setValue(personDetails);

At this point problem is Your are setting a User's E-mail as Child. But Fire-base does not allow this.

"@" and "." are invalid characters here

You can go to Fire-base console and check by manually adding data if You set an E-mail as Key fire-base will not allow it.

So You have to do some additional work with the email

In my case I managed it by removing the "@" and "." from the email.

id = email.replace("@", "-");
id = id.replace(".", "-");
courseRef.child(id).setValue(personDetails);

Upvotes: 1

Related Questions