【问题标题】:Angularfire2 snapshotchanges subscription pipe problemAngularfire2 snapshotchanges 订阅管道问题
【发布时间】:2019-03-06 19:23:03
【问题描述】:

我正在使用@angular/fire,我刚刚为数据库设置了 snapshotChanges(),现在我尝试订阅它以保持更新。

    return this.db.list<Plan>(this.BASE_PATH).snapshotChanges().subscribe(
      item => {
        return item.map(a => {
            var p = new Plan;
            p.key = a.payload.key;
            p.name = a.payload.child(this.NAME).val();
            p.descripton = a.payload.child(this.DESCRIPTON).val();
            p.isPublic = a.payload.child(this.ISPUBLIC).val() == 'false' ? false : true;

            return p;
        })
      }
    );

但这确实会导致async 管道出现问题并给我一个错误:

ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe'
    at invalidPipeArgumentError (common.js:3981)
    at 

错误实际上更长,但只是为了向您展示。

也许你有什么想法

【问题讨论】:

    标签: angular firebase angularfire2


    【解决方案1】:

    您应该使用.pipe 方法和map 运算符,而不是subscribe。如前所述,您不能在订阅中使用async 模板管道:

    return this.db.list<Plan>(this.BASE_PATH).snapshotChanges().pipe(
      map((items) => items.map(a => {
        const p = new Plan;
        p.key = a.payload.key;
        p.name = a.payload.child(this.NAME).val();
        p.descripton = a.payload.child(this.DESCRIPTON).val();
        p.isPublic = a.payload.child(this.ISPUBLIC).val() == 'false' ? false : true;
    
        return p;
      }))
    );
    

    【讨论】:

      【解决方案2】:

      您是否在模板中使用过异步管道?如果是这样,您不需要订阅 ts 类上的 snapshotChanges() 。 asyncPipe 会做到这一点

      【讨论】:

      • 好吧,我不知道这个
      • 将代码留到 snapShotChanges(),然后使用 asyncPipe.. 查看结果。让我知道它是否有效
      猜你喜欢
      • 2017-03-11
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 2018-04-04
      • 2018-06-25
      • 1970-01-01
      相关资源
      最近更新 更多