【问题标题】:Firebase - Updating a child valueFirebase - 更新子值
【发布时间】:2018-02-25 19:25:53
【问题描述】:

我正在尝试更新 Firebase 中的子值。

这是 HTML:

  <ion-item-group>
    <ion-item-divider color="light">We Need</ion-item-divider>
    <ion-item *ngFor="let list of lists" (click)="unlist()">{{list?.foodname}}</ion-item>
  </ion-item-group>

数组在此处的 TS 中声明:

export class HomePage implements OnInit {

  foods;
  lists;
  startAt = new Subject()
  endAt = new Subject()

TS中的函数是:

  unlist() {
    firebase.database().ref('/foods').child('state')
            .update({ state: "unlisted" });
  }

这是我的数据库

现在,触发此函数会将“状态”的子项添加到“食物”,而不是更改预先存在的状态。我需要做什么才能完成这项工作?

【问题讨论】:

    标签: firebase firebase-realtime-database


    【解决方案1】:
    /*  Here is the simple method */
    unlist() {
        from(foods).pipe(map(food => f.key), tap(key => {
            firebase.database().ref('/foods' + key).child('state')
                .update({
                    state: "unlisted"
                });
        })).subscribe(val => {
            console.log("Finished updating")
        });
    }
    
    /* Merging updates into One subscription */
    unlist() {
        merge(
            from(foods).pipe(map(food => f.key), map(key => {
                return fromPromise(firebase.database().ref('/foods' + key).child('state')
                    .update({
                        state: "unlisted"
                    }))
            }))
        ).subscribe(val => {
            console.log("Finished updating")
        })
    }
    

    我在这里使用了 Observable 库,因为您需要导入这些:

    import { merge } from 'rxjs/observable/merge';
    import { tap,map } from 'rxjs/operators';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多