Shubham Rajput
Shubham Rajput

Reputation: 55

Unable to store data in Firestore

I am new to Google Cloud Firestore service but while I was saving some data, it failed: document=sampleData,collection=info

private DocumentReference mdocRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    mdocRef=FirebaseFirestore.getInstance().collection("sampleData").document("info"); ....}

saving method:

private void saveInfo(){
    String Name=nameText.getText().toString().trim();
    String Phone=phoneText.getText().toString().trim();
    String Dob=dobText.getText().toString().trim();

    if(Name.isEmpty() || Phone.isEmpty() || Dob.isEmpty()){
        Toast.makeText(this,"Please enter the information ",Toast.LENGTH_SHORT).show();return;
    }
    Map<String,Object> data=new HashMap<String, Object>();
    data.put("name",Name);
    data.put("phone",Phone);
    data.put("dob",Dob);
    Log.i("phone",Phone);

    mdocRef.set(data).addOnCompleteListener(this, new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){
                Toast.makeText(getApplicationContext(),"Information has been saved! ",Toast.LENGTH_SHORT).show();return;

            }
            else{
                Toast.makeText(getApplicationContext(),"Information couldn't be saved!,Try again! ",Toast.LENGTH_SHORT).show();return;

            }
        }
    });

}

rule information on firestore:

 service cloud.firestore {
  match /databases/{database}/documents {
  match /sampleData/{anything=**}{
  allow read,write: if true;
  }
    match /{document=**} {
      allow read, write;
    }
  }
}

when user input data and app calls saveInfo() then task in :

public void onComplete(@NonNull Task<Void> task) {
        if(task.isSuccessful()){
            Toast.makeText(getApplicationContext(),"Information has been saved! ",Toast.LENGTH_SHORT).show();return;

        }
        else{
            Toast.makeText(getApplicationContext(),"Information couldn't be saved!,Try again! ",Toast.LENGTH_SHORT).show();return;

        }

is never successful.

Upvotes: 2

Views: 2239

Answers (1)

Hemant Parmar
Hemant Parmar

Reputation: 3976

Here is basic steps for read/write firebase value:

private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
//for offline data save
FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Inserting Data : The realtime database accepts multiple data types String, Long, Double, Boolean, Map, List to store the data. You can also use custom java objects to store the data which is very helpful when storing model class directly in database.

As every user needs a unique Id, you can generate one by calling push() method which creates an empty node with unique key. Then get the reference to ‘users’ node using child() method. Finally use setValue() method to store the user data.

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");

// Creating new user node, which returns the unique key value
// new user node would be /users/$userid/
String userId = mDatabase.push().getKey();

// creating user object
User user = new User("username", "[email protected]");

// pushing user to 'users' node using the userId
mDatabase.child(userId).setValue(user);

Here is User model :

@IgnoreExtraProperties
public class User {

    public String name;
    public String email;

    // Default constructor required for calls to
    // DataSnapshot.getValue(User.class)
    public User() {
    }

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }
}

Me too new in Firebase, i tried this for save basic data of User. hope it will help you.

Upvotes: 1

Related Questions