2

I have looked through the entire net... I have applied fireship tutorials and more. I cannot get data from firebase in angular. Please Help.

These are my three methods:

Getting data but its not being called in ngFor HTML.

getMessage(): AngularFireList<ChatMessage> {
  this.db.list('messages').valueChanges().subscribe(queriedItems => {
  console.log(queriedItems);

  });
  return this.queriedItems;
} 

Getting only one msg instead of all saved messages from firebase

 returnMessages(): AngularFireList<ChatMessage> {
   this.db.list('messages').valueChanges().subscribe(msg => {
     this.msg = msg;
     this.msgArr.push(msg)
     console.log("test push ", this.msgArr)
   });
   return this.msgArr;
 }

And this one gives me an ERROR (please see below)

getMessages(): AngularFireList<ChatMessage> {
  return this.db.list('messages', ref => {
    return ref.limitToLast(25).orderByKey();
  });
}

FeedComponent.html:2 ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' at invalidPipeArgumentError (common.js:4323) at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe._selectStrategy (common.js:4934) at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe._subscribe (common.js:4924) at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe.transform (common.js:4906) at Object.eval [as updateDirectives] (FeedComponent.html:2) at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911) at checkAndUpdateView (core.js:23307) at callViewAction (core.js:23548) at execComponentViewsAction (core.js:23490) at checkAndUpdateView (core.js:23313)

My feed component for calling the getMessage():

feed: AngularFireList<ChatMessage>;

constructor(private chat: ChatService) { }

ngOnInit() {
  console.log("feed initializing...")
  this.feed = this.chat.getMessages();
  console.log("Feed ", this.feed);
}

ngOnChanges() {
  this.feed = this.chat.getMessages();
}

And this is my HTML

<div class="feed">
  <div *ngFor="let message of feed | async" class="message">
    <app-message [chatMessage]=message></app-message>
  </div>
</div>
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
vince
  • 71
  • 2
  • 12
  • See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – str Dec 15 '19 at 10:33

1 Answers1

3

Change this:

getMessages(): AngularFireList<ChatMessage> {
  return this.db.list('messages', ref => {
    return ref.limitToLast(25).orderByKey();
  });
}

Into this:

getMessages() {
  return this.db.list('messages', ref => ref.orderByKey().limitToLast(25)).valueChanges();
}

Then change:

feed: AngularFireList<ChatMessage>;

constructor(private chat: ChatService) { }

ngOnInit() {
  console.log("feed initializing...")
  this.feed = this.chat.getMessages();
  console.log("Feed ", this.feed);
}

Into this:

feed;

constructor(private chat: ChatService) { }

ngOnInit() {
  console.log("feed initializing...")
this.chat.getMessages().subscribe(items => {
  console.log(items);
  this.feeds = items 
});
}

Change this:

<div class="feed">
  <div *ngFor="let message of feed | async" class="message">
    <app-message [chatMessage]=message></app-message>
  </div>
</div>

Into this:

<div>
  <div *ngFor="let message of feed" class="message">
    <app-message [chatMessage]=message></app-message>
  </div>
</div>

The async pipe is used with an observable.

The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted

feeds is of type AngularFireList which is not an observable.

Therefore add valueChanges() which returns an Observable of data as a synchronized array. Then inside the ngOnInit() subscribe to that observable and iterate inside the array feed in the ngFor

https://angular.io/api/common/AsyncPipe

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Peter, it worked! Thanks a bunch for the help. I need to change the sendMsg() a little around for it to work on both ends. Thank you very much for your help. – vince Dec 15 '19 at 19:16