Enter comments in an IONIC application

0

I'm creating an application in which under the content of the page I have the option of the user to make a comment. I am using the firebase to do registration and login and I even managed to save the message in the database, but only she needed to put the username or username and the date the comment was made.

My html looks like this:

 <ion-content #content id="content">
<ion-card *ngFor="let message of messages">
<ion-card-header>
  {{message.nome}}
</ion-card-header>
<ion-card-content>
  {{message.mensagem}}
</ion-card-content>

  

<ion-footer>
<ion-toolbar>
<ion-input placeholder="Comente algo..." [(ngModel)]="message"></ion-input>
<ion-buttons end>
  <button ion-button icon-right (click)="sendMessage()">
      Enviar
    <ion-icon name="send"></ion-icon>
  </button>
</ion-buttons>

My ts looks like this:

 export class Cap1SegObsPage {

 @ViewChild("content") content: any;
 username: string
 message: string = ""

 messages = [];

   constructor(public navCtrl: NavController) {
   this.getMessages();
    }

   getMessages(){
     var messagesRef = firebase.database().ref().child("mensSegObsCap1");
    messagesRef.on("value", (snap) => {
     var data = snap.val();
      this.messages = [];
      for(var key in data){
        this.messages.push(data[key]);
      }

      this.scrollToBottom();
    });
   }

   scrollToBottom(){
      var contentEnd = document.getElementById("content-end").offsetTop;
     this.content.scrollTo(0, contentEnd, 300);
  }

   sendMessage(){
    var messagesRef = firebase.database().ref().child("mensSegObsCap1");
     messagesRef.push({mensagem: this.message, nome: this.username});
     this.message = "";
   }
 }
    
asked by anonymous 25.08.2018 / 03:01

1 answer

0

When you log in to the user, you receive this user + token + other information on the success of the login call. What you probably have to do is save this user to localstorage, see: Ionic Storage or Ionic native storage Remembering that localstorage you have to save as a string using JSON.parse (code) and the JSON.stringfy (code) In the action of the click of insertion of a new comment, you catch who this user of the localstorage is and goes up to the firebase.

Remembering that there are other ways to do this

    
25.08.2018 / 14:51