javascript - 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
})
)
解决方案
推荐阅读
- python - Python pygame 模块问题
- c# - 将json数组反序列化为c#列表对象
- ios - 如何在暂停的后台获取中运行一些代码?
- java - 如何从主类使用/调用 KeyEvents 以在子类中使用
- javascript - 将参数设置为元素 id,然后在循环中更改其值
- d3.js - 某些地图路径的笔画是零散的
- sql - 带有美国假期的日期列表加上从上周六开始的每一天
- angular - 属性“CLUSTER_CLICK”不存在(角度)
- python - 在 TensorFlow 中上采样 5-D 张量
- elasticsearch - 为两天之间字段的百分位数差异创建一个 Elastic Watcher