'how I can return data as success message when I set data in firebase?

I try to return orderId after I set this order in Realtime database using angularfirestore what I should use after set() to return the orderId after sucess add this order to database

my order object is this:

const orderObj: Order = {
   payMethod: paymethod,
   arrivalHour: arrivalHour,
   tabel: this.tabel,
   itemsCount: Object.keys(this.handleCart.getCartData().items).length,
   state: { cook: false, serve: false, delete: false },
   items: this.handleCart.getCartData().items,
   orderId: new Date().toLocaleDateString(),
   addedOnDate: new Date().toLocaleDateString(),
   addedOnHour: new Date().toLocaleTimeString('en-US'),
   totalPrice: this.handleCart.getCartData().totalAmt,
};

and for set the data in database I use angular fire store as that :

 constructor(private store: AngularFirestore, private router: Router) {
   
store.collection('Orders').doc(this.data.Id).set(this.data)
  }


Solution 1:[1]

You can wrap the db operation with a try catch and handle accordingly.

try {
    store.collection('Orders').doc(this.data.Id).set(this.data)
    return {
      docId: this.data.id
    }
} catch (error) {
   return {
      error,
      errorMessage: 'something went wrong'
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafael Zasas