监听节点变化抛出错误

监听节点变化抛出错误

本文介绍了Angular2 fire 监听节点变化抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码正在运行,它应该监听节点中的变化并执行一个函数,但现在出现错误:

The code below was working and it should listen for changes in a node and execute a function but now am getting an error:

ncaught TypeError: Object(...) is not a function
at SwitchMapSubscriber.eval [as project] (changes.js:7)

所以,在我的 angular2 组件中,我有:

So, in my angular2 component I have:

private subscriptions = new Subscription();

registered: AngularFireList<any>;
constructor(private _af: AngularFireDatabase){
     this.registered = _af.list('/registered');
 }

ngOnInit() {
    this.subscriptions.add(
        this.registered.valueChanges().subscribe(
            res => {
                console.log("the value has changed");
            }
        )
    );
}

那么我哪里出错了,因为上面的错误指向:

So where am I going wrong as getting the error above which point to:

angular2fire/database/list/changes

我需要我的代码做的是在 firebase 节点发生变化时监听并登录到控制台

What I need my code to do is to listen to whenever there is a change in a firebase node and log to the console

订阅也被定义为:

    private subscriptions = new Subscription();

将它添加到订阅然后我可以使用 onDestroy 生命周期并防止内存泄漏,如下所示

Adding it to the subscriptions then I can use onDestroy lifecycle and prevent memory leaks as shown below

    ngOnDestroy() {
      this.subscriptions.unsubscribe();
    }

推荐答案

这是最近很流行的问题.请将 rxjs 5 升级到版本 6.

This is a popular issue these days. Please upgrade rxjs 5 to version 6.

发生这种情况是由于AngularFire 中的一些重大更改.升级后,该功能应按预期执行.

This happens due to some breaking changes in AngularFire. Once upgraded the function should perform as expected.

升级使用:npm install rxjs@6 rxjs-compat@6 --save(推荐)

或回滚(不推荐)使用:

npm uninstall angularfire2
npm install [email protected]
npm uninstall firebase
npm install [email protected]

查看 rxjs 官方 迁移文档 了解操作方法以及有关 5 和 6 之间更改的更多详细信息.

See the rxjs official migration doc for how-to's and more details on the changes between 5 and 6.

这篇关于Angular2 fire 监听节点变化抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:05