Reputation: 1144
Here is my code:
function saveID(sender_psid,count){
let data = new Object();
data.ID = sender_psid[count];
db.collection('users').doc('ID').set(data);
}
I am trying to add data to my firestore database. My current code does that but each time the function is called the new data replaces the old one instead of adding itself to the database. How do I fix that?
Upvotes: 0
Views: 1956
Reputation: 796
If you want to update the existing data, use update(data)
instead of set
If you want to put a new document there, you can use collection('users').add(data)
and it will use an auto generated id for the new document.
You can also add a new document to the collection by specifying a different document id to set: collection('users').doc(id).set(data)
. In this case, id has to be a variable. Basically if you use set on a document without the merge option, you are setting the data for the specified document id. So if you use the same document id, it will overwrite the document (unless you specify the merge parameter). If you use a different id, it will add a new document using your specified id.
Upvotes: 3