Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

javascript - Returning the $key from Angular Firebase straight after you've created the record

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));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This has been answered by: Get Key for object created after Push (Angular and Firebase)

I'll leave up the question just in case anyone else stumbles upon it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...