繁体   English   中英

从数据库Ionic3,角度和火力得到

[英]Ionic3, angular & firebase get from db

在聊天应用程序上工作。 将消息推送到数据库工作正常,但获取查询似乎无效。

ts文件看起来像这样,将消息发送到db即可正常工作。 获取消息不起作用。

ngOnInit(){
  this.encryptedMessages = this.getMessage();
}

getMessage(){
  return this.db.list('/messenger');
}

sendMessage(){
  this.db.list("/messenger").push({
  username: this.username,
  message: this.message
 }).then(()=>{
  //what happens once message is sent
 });
}

html看起来像这样。 使用* ngFor和异步管道显示从数据库检索到的所有消息。 屏幕上没有任何内容

<ion-content padding>
  <div id="message" *ngFor="let m of encryptedMessages | async">
    <div class="username">{{ m.username }}</div>
    <div class="message"> {{ m.message }} </div>
  </div>
</ion-content>

您还可以订阅到您的列表。

ngOnInit(){
  this.getMessage().snapshotChanges()
    .subscribe((actions) => {
       this.encryptedMessages = actions.map((action) => {
          const data = action.payload.val();
          data.key = action.key;
          return data;
       });
    });
}

getMessage(){
  return this.db.list('/messenger');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM