我正在基于Angular-firebase的聊天应用程序
ChatFormComponent.html:5错误TypeError:queryFn不是函数 在AngularFireDatabase.push ../ node_modules / angularfire2 / database / database.js.AngularFireDatabase.list中 (database.js:18) 在ChatService.push ../ src / app / services / chat.service.ts.ChatService.getMessages (chat.service.ts:39) 在ChatService.push ../ src / app / services / chat.service.ts.ChatService.sendMessage中 (chat.service.ts:29)
.ts文件
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文件
<input class="chatInput" [(ngModel)]="message"/>
<button class="chatButton" (click)=send()>Send</button>
服务代码->
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 = 'biky700@gmail.com';
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(){}
}
}
getMessages(): FirebaseListObservable<ChatMessage[]> { return this.db.list('messages', { query: { limitToLast: 25, orderByKey: true } }); }
当我将鼠标悬停在上面时,我得到下面的消息
**
[ts]类型为'{的参数:查询:{limitToLast:数字; orderByKey: 布尔值}; }”不能分配给类型为“ QueryFn”的参数。
对象文字只能指定已知的属性,而“查询”不能指定 存在于“ QueryFn”类型中。 (属性)orderByKey:布尔值
**
答案 0 :(得分:0)
某人似乎与您的问题相同,此问题已得到解答:Stackoverflow。
请尝试以下操作:
getMessages(): Observable<ChatMessage[]> {
return this.db.list('/messages', ref => {
ref.limitTolast(25).orderByKey(true)
});
}
答案 1 :(得分:0)
您的getMessage函数应该是这样的。
@Model.CmRlsd3D
有关更多信息,请参见此。 https://github.com/angular/angularfire2/blob/master/docs/rtdb/querying-lists.md