angular - Firestore first()).toPromise() always gives old value even I have re-run with new changes
问题描述
I have this method on a service:
getUserLink(uid: string): Promise<ConnectionLinkModel> {
return this.afs.doc<ConnectionLinkModel>
(`userConnectionLinks/${uid}`).valueChanges().pipe(first()).toPromise();
}
This is working fine for the first time. But let's say I have changed the Firestore data manually and re-run this method again (This is very important. I know since this is promise it'll not listen to the changes automatically. But why it doesn't give new value even though I have re-run it again.). But it never gives changed value. i.e. it always gives old value. How can I handle this use case?
ngOnInit(): void {
const res: ConnectionLinkModel = await
this.connectionLinkService.getUserLink(this.authService.currentUserUid);
}
Note: When Page loads it runs again. But it'll never give new value. Why?
解决方案
如果您启用了持久性,那么您将获得的第一个数据将始终来自本地缓存。
从文档:
要检查您是从服务器接收数据还是从缓存接收数据,请在快照事件中使用 SnapshotMetadata 上的 fromCache 属性。如果 fromCache 为真,则数据来自缓存并且可能是陈旧的或不完整的。如果 fromCache 为 false,则数据是完整的,并且是服务器上最新更新的最新数据。
getUserLink(uid: string): Promise<ConnectionLinkModel> {
return this.afs.doc<ConnectionLinkModel>(`userConnectionLinks/${uid}`)
.valueChanges()
.pipe(debounceTime(1000)).pipe(first()).toPromise();
}
推荐阅读
- math - 仅使用度数使物体绕另一个轨道运行(不是仅弧度整数数学)
- javascript - 带有 exec ruby 的异步函数
- postgresql - 如何根据日期选择 postgres 表中的行并将其存储在新表中?
- c# - 带有 SelectMany() 的平面数据,如左连接
- bluetooth - 使用 GnuRadio 解码蓝牙信号和数据包
- mfc - 验证列表控件就地编辑
- matlab - 标准化图像时尺寸错误
- android - 在 Play 商店分阶段推出中,该百分比仅适用于现有用户还是新用户和现有用户?
- codeigniter - 未找到 CI_Template
- vb.net - 如何使用标签或文本框构建和打开组合 url