Insert data and "primary key" into Firebase

Asked

Viewed 754 times

3

Good morning,

I’m starting with programming, and I have the following question. 1. I can enter data in firebase, but when I try to enter it again only happens to replace the data instead of entering the new record.

  1. How can I enter data into firebase along with the "primary key"? (field highlighted in the image, it is only to display the field I desire, it is not my bank in the firebase).

Imagem de banco no firebase com o campo "chave primária" Hugs.

2 answers

0

Try this.

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
 mDatabase.child("tasks").push().setValue(firebaseData, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            //Problem with saving the data
            if (databaseError != null) {

            } else {
                //Data uploaded successfully on the server

               }

            }
    });

0

I don’t quite understand your question but I’ll try to answer.

Primary key can be informed by you or can be generated by Firebase.

To enter a record stating the key: db.Collection("users"). doc("my user’s key"). set(objectComInformationDoUsuario);

To enter a record without entering the key (key is generated by Firebase): db.Collection("users"). doc(). set(objectComInformationDoUsuario); In this case, the return of this method will be an object containing, among other things, the key created.

To update the registry you need the key. To get the key: var referenceDoUsuario = db.Collection("users"). doc("my user’s key");

With that in hand, just do referenceDoUsuario.update({ name: "User name" })

Note: I don’t know on which platform you are developing. I used as a basis the web (Javascript). Documentation: https://firebase.google.com/docs/firestore/manage-data/add-data

Browser other questions tagged

You are not signed in. Login or sign up in order to post.