Reputation: 37
How can I set id of post that I'm adding? I thought that getItemNextKey()
returns id that will be assigned for the post, but it's not.
AddItem(data, downloadURLs) {
data.id= this.getItemNextKey(); // Persist a document id
data.upload = downloadURLs;
// console.log('this.uploadService.downloadURLs: ' + downloadURLs);
// console.log('data.upload: ' + data.upload);
this.db.collection('items').add(data);
}
Upvotes: 0
Views: 358
Reputation: 37
I did this and it works now.
// Add a new document with a generated id
var addDoc = this.db.collection('items').add(data).then(ref => {
var updateNested = this.db.collection('items').doc(ref.id).update({
id: ref.id
});
});
Upvotes: 1
Reputation: 4451
As stated in the official docs
When you use set() to create a document, you must specify an ID for the document to create. For example:
db.collection("cities").doc("new-city-id").set(data);
If you dont want to set an ID yourself you can use add
But sometimes there isn't a meaningful ID for the document, and it's more convenient to let Cloud Firestore auto-generate an ID for you. You can do this by calling add():
Upvotes: 0