对象文字只能指定已知的属性,而'message'在类型'ChatMessage []'

时间:2018-10-31 21:11:16

标签: typescript angular6

我对Angular 6代码感到困惑,我想知道您是否可以帮助我解决此问题...

chat-message.model.ts:

export class ChatMessage {
    $key?: string;
    email?: string;
    userName?: string;
    message?: string;
    timeSent?: Date = new Date();
}

chat.service.ts:

export class ChatService {
  user: any;
  chatMessages: AngularFireList<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;
    //   }
    // });
  }

  sendMessage(msg: string) {
    const timestamp = this.getTimeStamp();
    // const email = this.user.email;
    const email = 'test@example.com';
    this.chatMessages = this.getMessages();
    this.chatMessages.push({
      message: msg,
      timeSent: timestamp,
      // userName: this.userName,
      userName: 'test-user',
      email: email    
    });

    console.log('Called sendMessage()!');

  }

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

我在“ message:msg”行和“ query”上出现chat.service.ts错误。 在“ message:msg”行上说:

Argument of type '{ message: string; timeSent: string; userName: string; email: string; }' is not assignable to parameter of type 'ChatMessage[]'.
  Object literal may only specify known properties, and 'message' does not exist in type 'ChatMessage[]'.

在查询中显示:

Argument of type '{ query: { limitToLast: number; orderByKey: boolean; }; }' is not assignable to parameter of type 'QueryFn'.
  Object literal may only specify known properties, and 'query' does not exist in type 'QueryFn'.

0 个答案:

没有答案