首页 > 解决方案 > Angular 和 Firebase 在值变化中获取可观察的数据

问题描述

获取数据多次正确执行函数的问题,这些函数在 ngOnInit 中执行一次,但我不知道在服务器中会出现这些问题,我在 snapshotChanges 中处理但我不知道。

谢谢帮助

https://i.stack.imgur.com/EinQg.png

return <Observable<Products[]>> t.db.collection(PATHS_FIRESTORE.products).snapshotChanges()
    .pipe(
      map(actions => {
        let arr = actions.map((res) => {                
          let doc: any = <any>res.payload.doc.data()

          let obj: any = {}

          if (!isNullOrUndefined(cart)) {                            
            for (const prod in cart) {                    
                if (cart.hasOwnProperty(prod)) {
                    const element = cart[prod];
                    if (doc.uid === prod) {
                        obj[doc.uid] = {
                            name_product: doc.name_product,
                            path_img: doc.path_img,
                            price: doc.price,
                            quantity: doc.quantity + element.total,
                            uid: doc.uid,
                            uid_local: doc.uid_local
                        }
                    } else {
                        t.db.collection(PATHS_FIRESTORE.products).doc(prod).ref.get().then( res => {
                            const data = res.data()

                            return obj[res.id] = {
                                name_product: data.name_product,
                                path_img: data.path_img,
                                price: data.price,
                                quantity: element.total,
                                uid: doc.uid,
                                uid_local: doc.uid_local
                            }
                        })
                    }                        
                }
                console.log(obj)
            }
            return obj
          }else {
            obj = {                  
                ...doc
            }

            return obj
          }
        })
        .filter((b: any) => {
            return b.uid_local === uid_local
        })
        .filter((b: any) => {                
            return b.quantity > 0
        })
        .filter((b: any) => {
            return !b.status 
        })

        console.log(arr)

        return arr
      })
    )

标签: javascriptangulartypescriptfirebase

解决方案


推荐阅读