D
D
Darkness2019-05-25 12:45:38
firebase
Darkness, 2019-05-25 12:45:38

Is it possible through .set() to create new documents automatically?

Hi, I would like to know if it is possible to create new documents in the collection instead of overwriting them? Now at me one document is created, and there I send the data necessary to me. But if I repeat the steps from the page again, then this data will overwrite the previous one. How can this problem be solved? I would be very grateful for your help!
I'm using firestore, not dataBase.

let dbUD = firebase.firestore();
    
   const docRef = dbUD.collection("data").doc("id").set({
   email: response.user.email,
   uid: response.user.uid,
})
   .catch(function(error) {
     console.error("Error writing document: ", error);
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Darkness, 2019-05-25
@AntonBrock

I spent almost 3 hours looking for this answer, and the answer was firstly in the documentation (which I read with my eyes closed, it seems), and secondly in front of my nose.
To create random documents, you just need to leave the .doc() field EMPTY when requesting, that's all. Firebase will create an id for it and assign the data. And there's nothing else to do...

const docRef = dbUD.collection("data").doc().set({})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question