wesdoyle / base-chat

A simple chat application built using Angular and Firebase
52 stars 52 forks source link

The query Error won't go. #10

Open MadaZZ opened 6 years ago

MadaZZ commented 6 years ago

import { Injectable } from '@angular/core'; //import { AngularFireDatabase } from 'angularfire2/database'; import { AngularFireDatabase,FirebaseListObservable } from 'angularfire2/database-deprecated'; 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: any; chatMessages: FirebaseListObservable<ChatMessage[]>; chatMessage: ChatMessage; userName: Observable;

constructor( private db: AngularFireDatabase, private afAuth: AngularFireAuth){ this.afAuth.authState.subscribe(auth=>{ //We have set user to auth if it is not undefined or null if(auth !== undefined && auth !== null){ this.user=auth; } }); }

//Function to send message sendMessage(msg:string){ var timestamp= this.getTimeStamp(); var email= this.user.email; this.chatMessages=this.getMessages(); this.chatMessages.push({ message: msg, timeSent: timestamp, userName: this.userName, email1: email }); }

//Function to get timeStamp getTimeStamp(){ const now = new Date(); const date = now.toLocaleDateString(); const time = now.toLocaleTimeString(); return(date+ ' ' + time); }

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

Above is my complete chatService code (still corrections required in getTimeStamp() ) But the error in the query won't go. Please help me out asap.

toufikImk commented 6 years ago

I had the same issue but it's solved now:

//change getMessages in chat.service.ts to be like this getMessages():FirebaseListObservable<ChatMessage[]>{ return this.db.list('Messages', { query :{ limitToLast:25, orderByKey:true } }); } // in chat.service.ts and app.module.ts you have to make import {AngularFireDatabaseModule} from'angularfire2/database'; //look like this import {AngularFireDatabaseModule} from 'angularfire2/database-deprecated';

Lertis commented 5 years ago

Hi @MadaZZ, My dependencies are: "firebase": "5.8.1", "angularfire2": "5.1.1";

I changed: return this.db.list('messages', { query: { limitToLast: 25, orderByKey: true } }); to: return this.db.list('messages', ref => ref.orderByKey().limitToLast(25));