0
votes

I am working on Angular-firebase based chat Application ,

  • I am getting error when I send data to my Angular service file chat.service.ts
  • I am putting error which I am getting below

ChatFormComponent.html:5 ERROR TypeError: queryFn is not a function at AngularFireDatabase.push../node_modules/angularfire2/database/database.js.AngularFireDatabase.list (database.js:18) at ChatService.push../src/app/services/chat.service.ts.ChatService.getMessages (chat.service.ts:39) at ChatService.push../src/app/services/chat.service.ts.ChatService.sendMessage (chat.service.ts:29)

  • I am putting code of the component which send data to the service file below

.ts file

import { Component, OnInit } from '@angular/core';
import { ChatService } from '../services/chat.service';

@Component({
  selector: 'app-chat-form',
  templateUrl: './chat-form.component.html',
  styleUrls: ['./chat-form.component.css']
})
export class ChatFormComponent implements OnInit {

message: string;

  constructor( private _chat: ChatService) {}

  send() {
    console.log(this.message);
    this._chat.sendMessage(this.message);
    this.message = '';
  }
}

.html file

<input class="chatInput" [(ngModel)]="message"/>

<button class="chatButton"  (click)=send()>Send</button>
  • the above component code sends data to the service which Interact to the firebase

service code ->

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 ) { }

getUser() {}

  sendMessage(msg: string) {
    const timestamp = this.getTimeStamp();
    const email =  '[email protected]';
    this.chatMessages = this.getMessages();
    this.chatMessages.push({
      message: msg,
      timeSent: timestamp,
      userName:  'Anurag Ranjan',
      email: email });
  }

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

  getTimeStamp(){}
 }
}

Important

  • In the above Service code you can see some part of code which create our message feed binding , code given below
getMessages(): FirebaseListObservable<ChatMessage[]> {
    return this.db.list('messages', {
      query: {
        limitToLast: 25,
        orderByKey:  true
      }
    });
  }

When I hover on this I am getting a meassage given below

**

[ts] 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'. (property) orderByKey: boolean

**

2

2 Answers

0
votes

someone seems to have the same problem as yours, it has been answered in this post: Stackoverflow.

please try this:

getMessages(): Observable<ChatMessage[]> {
return this.db.list('/messages', ref => {
  ref.limitTolast(25).orderByKey(true)
});

}

0
votes

You getMessage function should be something like this.

getMessages(): Observable<ChatMessage[]> {
    return this.db.list('/messages', ref => 
      let q = ref.limitTolast(25).orderByKey(true);
      return q;
    );
  }

For more info, please see this. https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md