Reputation:
I call a method every time button add is clicked. In this method I create a new document for my collection which contains a title and an array of strings. The creation of this document get done in my method by calling a method from my saved service. I want to add a last String value field to my document when complete button is clicked. How can I get a snapshot of the current saved id in order to add a new element? I am using Angular and firestore
These buttons are located in component.html
<button type="button" class="btn btn-success" (click)="onSave()">Save</button>
<button type="button" class="btn btn-success" (click)="onComplete()">Complete</button>
OnSave and onComplete are located in component.ts
onSave() {
const btn = document.getElementById("addtopic");
this.modalService.hide(1);
// Get starting date
this.startTopicTimestamp= firebase.firestore.Timestamp.fromDate(new Date());
console.log(`Starting time : ${this.startTopicTimestamp}`);
btn.innerHTML = "End topic";
btn.setAttribute('class', 'btn btn-danger');
// New document creation
this.savedService.savedDoc(this.topicTitle, this.myTags, this.startTopicTimestamp);
}
onComplete(){
// Here we should get a snapshot of the current saved id
// To make this function work
this.savedService.addElement(this.saved.id)
}
This method is part of savedService
public savedDoc(title: string, tags: string[], date: Date): void {
const savedFields = {
title: title,
startTime: date,
tags: tags
}
this.db.collection('saved').add(savedFields);
}
Upvotes: 0
Views: 383
Reputation: 2127
According to this (Firestore - How to get document id after adding a document to a collection), you can just do this:
public savedDoc(title: string, tags: string[], date: Date): void {
const savedFields = {
title: title,
startTime: date,
tags: tags
}
return this.db.collection('saved').add(savedFields).then(function(docRef) {
return docRef.id;
});
}
Because add
returns a DocumentReference
Upvotes: 1