In Angular connecting with a Firebase realtime db I'm looking for a way to retrieve the $key straight after I've created the record.
The only current way I can think of is to add an extra field that adds the datetime when you create the record, then retrieving the last record sorted by datetime. I'm pretty sure this isn't the most efficient way to do it, but it's currently the only way I can think of at the moment.
I've simplified my code below in the hope that it's quicker to read. (assume that the constructor and everything else is correct)
Registrant.ts
export class Registrant {
$key: string;
salutation: string;
}
registrant.service.ts
createBooking(apt: Registrant) {
return this.bookingListRef.push({
salutation: apt.salutation
});
register.page.ts
formSubmit() {
this.aptService.createBooking(this.bookingForm.value).then(res => {
console.log("the $key that's just been generated in firebase for this record: ");
this.bookingForm.reset();
})
.catch(error => console.log(error));
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…