Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 订阅角度错误_Javascript_Angular_Subscribe - Fatal编程技术网

Javascript 订阅角度错误

Javascript 订阅角度错误,javascript,angular,subscribe,Javascript,Angular,Subscribe,我正在尝试解决这个问题,因为我无法登录我的应用程序。 我在关注youtube教程,因为我是angular的新手。 这是所有者的github及其项目-> 它给了我以下错误: chat.service.ts的代码: import { Injectable } from '@angular/core'; import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database'; import { Angu

我正在尝试解决这个问题,因为我无法登录我的应用程序。 我在关注youtube教程,因为我是angular的新手。 这是所有者的github及其项目->

它给了我以下错误:

chat.service.ts的代码

import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs/Observable';
import { AuthService } from '../services/auth.service';
import * as firebase from 'firebase/app';

import { ChatMessage } from '../models/chat-message.model';

@Injectable()
export class ChatService {
  user: firebase.User;
  chatMessages: FirebaseListObservable<ChatMessage[]>;
  chatMessage: ChatMessage;
  userName: Observable<string>;

  constructor(
    private db: AngularFireDatabase,
    private afAuth: AngularFireAuth
    ) {
        this.afAuth.authState.subscribe(auth => {
          if (auth !== undefined && auth !== null) {
            this.user = auth;
          }

          this.getUser().subscribe(a => {
            this.userName = a.displayName;
          });
        });
    }

  getUser() {
    const userId = this.user.uid;
    const path = `/users/${userId}`;
    return this.db.object(path);
  }

  getUsers() {
    const path = '/users';
    return this.db.list(path);
  }

  sendMessage(msg: string) {
    const timestamp = this.getTimeStamp();
    const email = this.user.email;
    this.chatMessages = this.getMessages();
    this.chatMessages.push({
      message: msg,
      timeSent: timestamp,
      userName: this.userName,
      email: email });
  }

  getMessages(): FirebaseListObservable<ChatMessage[]> {
    // query to create our message feed binding
    return this.db.list('messages', {
      query: {
        limitToLast: 25,
        orderByKey: true
      }
    });
  }

  getTimeStamp() {
    const now = new Date();
    const date = now.getUTCFullYear() + '/' +
                 (now.getUTCMonth() + 1) + '/' +
                 now.getUTCDate();
    const time = now.getUTCHours() + ':' +
                 now.getUTCMinutes() + ':' +
                 now.getUTCSeconds();

    return (date + ' ' + time);
  }
}

您应该使用this.chat,而不仅仅是在构造函数内部聊天

constructor(chat: ChatService) {
    this.chat.getUsers().subscribe(users => {
      this.users = users;
    });
  }

您是否在app.module.ts文件中导入了
聊天服务
,并将其添加到
提供商

import { ChatService } from './services/chat.service';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  providers: [
    ...
    ChatService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
如果是这样,请尝试在构造函数中的
chat
之前添加
private

constructor(private chat: ChatService) {
  chat.getUsers().subscribe(users => {
    this.users = users;
  });
}

同时显示您的用户列表组件,它是发生错误的地方。@GabrielBitencourt完成。我添加了valuechanges();这似乎是固定的,但只有一个文件。你仍然没有回答我的问题!错误是什么对不起,它生成了一个新错误->类型“AngularFireObject”上不存在属性“subscribe”。在chat.service.ts.So中,我应该保留valuechanges()?
返回这个.db.object(路径)
返回的是一个
AngularFireObject
,而不是一个可观察的
,所以是的。此外,还应该为函数定义返回类型,如
getUser():AngularFireObject{
,以便IDE可以尽早捕获任何错误。(需要在顶部导入
User
constructor(private chat: ChatService) {
  chat.getUsers().subscribe(users => {
    this.users = users;
  });
}