首页 > 解决方案 > firebase 查询在客户端返回部分更新的节点

问题描述

由于某些原因,我遇到了无法重现的错误

每次收到添加/更新/删除的数据时,我都会根据当前时间戳触发一个新的 Firebase 查询

一个节点有这个模式

$UID:
  color:
  description:
  infos:
  temperature :
    low:
    high:
  timestamp:
  title:
  update:

因此,在我的应用程序中,我用鼠标移动了一个图形对象并将值更新为 db,如下所示:

this.editor.updates[`${this.uid}/update`] = true;
this.editor.updates[`${this.uid}/add`] = null;
this.editor.updates[`${this.uid}/infos`] = "my new infos";
this.editor.updates[`${this.uid}/timestamp`] = Date.now();
// then I send `this.editor.updates` object to the realtime database

我正在使用的侦听器是snapshotChanges如此立即检索 eventType 的对象child_added

这可能是正常的,因为这是向侦听器发出的第一个值,但唯一的问题是我得到了一个仅包含更新的属性的部分对象infostimestamp并且该对象还update应该包含temperature,color和.titledescription

看起来问题可能来自这样一个事实,即我立即在订阅中触发了一个新查询,这导致了奇怪的行为。这是我的递归查询

在我的主要组件的 ngOnInit 里面

public date$ = new BehaviorSubject(null);
...
this.date$.pipe(
  switchMap(
    (date) => {
      this.currQuery = this.db.getActionsStartAtNow('path', date)
      return (this.currQuery);
    }
  )
).subscribe(
  (actionsList:any[]) => {
    //process the data
    //then trigger updated query
    this.date$.next(Date.now())
  }
)

在我的数据库服务中

import { AngularFireDatabase } from '@angular/fire/database';
import { Observable } from 'rxjs';
import 'rxjs/add/observable/empty';

  constructor(private db:AngularFireDatabase) {
  }


...

  getActionsStartAtNow(path:string, date?:number){ 
    return (date ? this.db.list(path, (ref) => {
          return (ref.orderByChild('timestamp').startAt(date))
        }
      ).snapshotChanges() : Observable.empty()
    )
  }

错误的日志

对象{信息:“0-1602194400000-1602626400000-0-0-1-0”,时间戳:1605429748370,更新:真}

正确的日志

对象{颜色:3,描述:“”,信息:“0-1602194400000-1602626400000-0-0-1-0”,温度:{…},时间戳:1605429748370,标题:“”,更新:真}

标签: javascriptangularfirebasefirebase-realtime-databaseangularfire

解决方案


推荐阅读