@Injectable({
providedIn: 'root'
})
export class ChatService {
config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
app = initializeApp(this.config);
database = getDatabase(this.app);
messagesRef = ref(this.database, "messages");
constructor() {}
sendMessage(newMessage: string) {
if (newMessage.trim()) {
push(this.messagesRef, {
text: newMessage,
createdAt: new Date().toString()
});
}
}
getMessages(): Observable<any[]> {
return new Observable((observer) => {
onValue(this.messagesRef, (snapshot) => {
const data = snapshot.val();
const messages = data ? Object.values(data) : [];
observer.next(messages);
});
});
}
}
so I’m trying to implement a real-time database of firebase to my service typescript file but this error appears : Property ‘list’ does not exist on type ‘DatabaseReference’.
any solution ?
I tried to change the code but I got a similar error ( Property ‘list’ does not exist on type ‘Database’
2
Answers
You’re configuring Firebase with its regular JavaScript SDK in your code, but then try to use the
list()
method which only exists in the AngularFire wrapper library.If you want to use the AngularFire API, initialize and inject it as shown in the AngularFire developers guide for Realtime Database. From there:
You seem to have missed the first and the last part of that.
The above answer (from @puf-FrankvanPuffelen) does resolve your error but doesn’t resolve your problem completely though.
As mentioned in the answer earlier, you missed providing the Database instance in the application and injecting the database into your component/service.
Only thing left is to get the DataSnapshot changes as an observable and subscribe to it
Your Message Service
And subscribe to the observable in the component