You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular Firestore中监听特定键值变化问题排查——getUserEventSummaryE函数非目标字段变更时误触发

Fixing Firestore Listener to Only Trigger on Specific Field Changes

Hey there! I see the issue here—your current listener is comparing the entire document object for changes, which is why any field update (even unrelated ones like userbalance or dob) is triggering a new emission. Let's adjust this to only react when the playing_summary field changes.

The Problem with Your Current Code

Your distinctUntilChanged operator uses _.isEqual(prev, curr) to compare full document objects. This means any field modification will make the comparison return false, causing the stream to emit the updated document—even when the change has nothing to do with playing_summary.

The Solution

Modify the distinctUntilChanged comparison function to only check if the playing_summary field has changed, instead of validating the entire document:

getUserEventSummaryE(userId) {
  return docData(doc(this.firestore, `/user/${userId}/event_summary/current/`), {idField: 'playing_summary'})
    .pipe(
      // Only compare the playing_summary field between previous and current values
      distinctUntilChanged((prev, curr) => _.isEqual(prev.playing_summary, curr.playing_summary))
    );
}

How This Works

  • We still fetch the complete document data with docData, so you’ll get the full document whenever playing_summary changes.
  • The distinctUntilChanged operator now only checks if the playing_summary field is identical (deeply, thanks to Lodash’s _.isEqual) between the previous and current document state.
  • Updates to other fields like userbalance or dob will result in _.isEqual(prev.playing_summary, curr.playing_summary) returning true, so those irrelevant changes are filtered out and won’t trigger your subscription.

Quick Notes

  • If playing_summary is a simple value (string, number, boolean), you could replace _.isEqual with a strict equality check (prev.playing_summary === curr.playing_summary). But _.isEqual is safer long-term—it handles deep comparisons automatically if playing_summary ever becomes a nested object or array.

内容的提问来源于stack exchange,提问作者cakePHP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:02:28