简体   繁体   中英

Get response after set data on firebase ionic 3

Following code for set data on firebase database. I want to know how to get successfully saved details after set in firebase?

 //get current time fire base
  getCurrentDate() {
    //Client Meeting model
    let oCurrentDateModel = new current_date_model();
    oCurrentDateModel.current_date_time_stamp = firebase.database.ServerValue.TIMESTAMP;

    return new Promise<any>((resolve, reject) => {
      this.oAngularFireDatabase.database.ref('/Settings')
        .child('CurrentDate')
        .set(oCurrentDateModel)
        .then(
          data => {

            //THIS DATA IS UNDEFINED
            //HERE I WANT TO GET SAVED current_date_time_stamp

            console.log(data);
            //Alert Generation
            this.oAlertProvider.showAlert('Success', "Updated Successful");            
          },
          err => {
            reject(err);
            //Alert Generation
            this.oAlertProvider.showAlert('Error', err.message);
          })
    })
  }

To get the value of a server-side timestamp, you will have to attach a listener:

let timestampRef = this.oAngularFireDatabase.database.ref('/Settings/CurrentDate/current_date_time_stamp');
let timestampListener = timestampRef.on("value", function(snapshot) {
  if (snapshot.exists()) {
    console.log(snapshot.val());
    timestampRef.off("value", timestampListener);
  }
});

This snippet of code also removes the listener once it has gotten the timestamp value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM