我使用 Firebase 开发 Ionic 3.9 聊天,但出现以下错误:
TypeError: this.db.list(...).subscribe is not a function
这是我的代码:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AngularFireDatabase } from 'angularfire2/database';
@IonicPage()
@Component({
selector: 'page-consersation',
templateUrl: 'conversation.html',
})
export class ConversationPage {
username: string = '';
message: string = '';
_chatSubscription;
s;
messages;
constructor(public db: AngularFireDatabase,
public navCtrl: NavController, public navParams: NavParams) {
this.username = this.navParams.get('username');
this._chatSubscription = this.db.list('/conversation').subscribe( data => {
this.messages = data;
});
}
sendMessage() {
this.db.list<any>('/conversation').push({
username: 'romain',
message: this.message
}).then( () => {
// message is sent
});
this.message = '';
}
}
你能帮我吗?
最佳答案
在 this.db.list('/conversation').subscribe(
中,您缺少 .list(...)
和 之间的某些内容.subscribe(...
您缺少的是 .valueChanges()
或 .snapshotChanges()
...您可以在 the AngularFire2 documentation here 阅读有关差异的信息。 .
我通常最常使用 .valueChanges(),因此对于 .valueChanges() 的快速示例,您的代码将是:
this._chatSubscription = this.db.list('/conversation').valueChanges().subscribe( data => {
this.messages = data;
);
编辑 - 更正了下面的代码。不应该设置等于整个 .subscribe
的变量...定义您的指针/监听器,然后单独订阅它。
this._chatSubscription = this.db.list('/conversation').valueChanges()
this._chatSubscription.subscribe( data => {
this.messages = data;
);
第二次编辑 - 在OP作为答案发布的新错误消息之后。 这个新错误看起来是由于版本冲突造成的 - 请查看 this question with multiple possible solutions 。
关于javascript - 类型错误:this.db.list(...).subscribe 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51178838/