How to remove method "Add" after navigate to another page in Angular 9

Viewed 212

I have a page with a list of products, add them to the order, then go to the list of orders and trying to delete the entire order, all documents are added again, after refreshing the page it does not happen. It is as the method from the list page was still in memory. I try OnDestroy but nothing changes. How to remove method "Add" after navigating to another page. Method "Add" in List of products.

  addToCard(p) {
    this.auth.user$.subscribe(ud => {
      if (ud !== null) {
        const dfecha = new Date(Date.now());
        const dotime = dfecha.toLocaleTimeString();
        const orderef = this.dataRef(ud.uid);
        const dprice = p.price;
        const dquant = p.quant || 1;
        const pricetotal = dprice * dquant;
        this.osrv.getOrderOpenOut(ud.uid, this.biident).subscribe(KX => {
          if (KX.length === 0) {            
            orderef.add({
              data...
            }).then((ref) => {
              this.orderid = ref.id;
              orderef.doc(ref.id).collection('products').doc(p.id).set({
                prodcode: p.code,
                prodid: p.id,
                prodnam: p.name,
                prodquant: this.OrderQuant || 1,
                prodmax: p.maxpedido,
                prodprice: p.price,
                totalprice: pricetotal,
              });
            });
          } else {
            this.osrv.getOrderOpenOut(ud.uid, this.biident).subscribe(xId => {
              const nOrdId = xId[0];
              orderef.doc(nOrdId).collection('products').doc(p.id).set({
                  prodcode: p.code,
                  prodid: p.id,
                  prodnam: p.name,
                  prodquant: this.OrderQuant || 1,
                  prodmax: p.maxpedido,
                  prodprice: p.price,
                  totalprice: pricetotal,
                });
            });
          }
        });
        this.snack.open('El producto se ha añadido al pedido', 'OK', {duration: 2000});
      } else {
        const dialogRef = this.dialog.open(LoginDialogComponent, {
          width: '320px',
        });
      }
      });
  }

And Method Detele in Order List:

  deleteOrder(e) {
    const order = this.db.collection('users').doc(this.userid).collection('ordersout').doc(e.id);
    const quantprodu = order.collection('products');
    quantprodu.get().forEach(ilp => {
      if (ilp.size === 0) {
        order.delete();
      } else {
        quantprodu.get().forEach(indprod => {
          indprod.forEach(prd => {
            quantprodu.doc(prd.id).delete();
          });
        }).then(() => {
          order.delete();
        }).then(() => {
          this.snap.open('El pedido se ha eliminado de la lista', 'OK', {duration: 2000});
          this.backToHome();
        });
      }
    });
  }

Some comoponents i show using ng-template and some using routing. Deleting last Order i received this error.

ERROR FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined
    at new n (http://localhost:4200/vendor.js:165025:23)
    at dr (http://localhost:4200/vendor.js:173857:15)
    at hr (http://localhost:4200/vendor.js:173802:5)
    at n.doc (http://localhost:4200/vendor.js:179537:50)
    at AngularFirestoreCollection.doc (http://localhost:4200/vendor.js:86736:54)
    at SafeSubscriber._next (http://localhost:4200/main.js:1922:37)
    at SafeSubscriber.__tryOrUnsub (http://localhost:4200/vendor.js:273576:16)
    at SafeSubscriber.next (http://localhost:4200/vendor.js:273515:22)
    at Subscriber._next (http://localhost:4200/vendor.js:273465:26)
    at Subscriber.next (http://localhost:4200/vendor.js:273442:18)

but the products to the order are added again

1 Answers

It could be the case that cached data setup for your Firestore instance is not properly set and that could be causing the issue you are facing, you can check more details on how to properly set it up on this documentation.

That being said, you could, as a workaround, replace in your front-end the router-link angular component from a plain html href.

Related