简体   繁体   English

TypeError:this.db.list(...)。subscribe不是一个函数

[英]TypeError: this.db.list(…).subscribe is not a function

I develop an Ionic 3.9 chat with Firebase and I have the following error: 我与Firebase开发了Ionic 3.9聊天,并且出现以下错误:

TypeError: this.db.list(...).subscribe is not a function TypeError:this.db.list(...)。subscribe不是一个函数

Here is my code: 这是我的代码:

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 = '';
    }
}

Can you help me please? 你能帮我吗?

In this.db.list('/conversation').subscribe( you are missing something between the .list(...) and the .subscribe(... this.db.list('/conversation').subscribe(您缺少.list(...).subscribe(...之间的某些内容

What you're missing is either .valueChanges() or .snapshotChanges() ... You can read about the differences at the AngularFire2 documentation here . 您所缺少的是.valueChanges().snapshotChanges() ...您可以在AngularFire2文档中阅读有关差异的信息

I typically use .valueChanges() most often, so for a quick example with .valueChanges() your code would be: 我通常最常使用.valueChanges(),因此对于一个带有.valueChanges()的快速示例,您的代码将是:

this._chatSubscription = this.db.list('/conversation').valueChanges().subscribe( data => {
    this.messages = data;
);

EDIT - corrected code below. 编辑 -下面的更正代码。 Not supposed to set a variable equal to the whole .subscribe ... Define your pointer/listener, and then subscribe to it separately. .subscribe将变量设置为等于整个.subscribe ...定义您的指针/侦听器,然后分别对其进行订阅。

this._chatSubscription = this.db.list('/conversation').valueChanges()
this._chatSubscription.subscribe( data => {
    this.messages = data;
);

2nd EDIT - after new error message that OP posted as answer. 第二次编辑 -OP发布新的错误消息作为答案之后。 That new error looks like it's due to version conflicts - check out this question with multiple possible solutions . 该新错误似乎是由于版本冲突引起的- 使用多种可能的解决方案来检查该问题

When you're getting the following error polyfills.js:3 Uncaught TypeError: Object(...) is not a function... , try the following code below: 当您遇到以下错误polyfills.js:3 Uncaught TypeError: Object(...) is not a function... ,请尝试以下代码:

this._chatSubscription = this.db.object('/conversation').valueChanges().subscribe(data => {
  this.messages = data;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM