Pushing Data to Firebase under AuthID(link user and their message ? )[solved]


#1

I’m trying to add data to firebase under user authid.Right now firebase is generating its own keys. I mean I’m trying to connect sent messages to the user who sent it.(authenticated user)

Right now db is looking like this:

enter image description here

This is how I add message. There is a model file in models folder.

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase,FirebaseListObservable} from 'angularfire2/database';
import {Message} from '../../models/message';


@Component({
  selector: 'page-newpost',
  templateUrl: 'newpost.html'
})
export class NewPostPage {
  message = {} as message;

    messageRef$ : FirebaseListObservable<Item[]>;

  constructor(private fire:AngularFireAuth,private db :AngularFireDatabase,public navCtrl: NavController) {
    this.messageRef$ = this.db.list('messages');



}


sendPost(message:message){

  const promise =  this.messageRef$.push({
          message: message.message,
          //location: Number(message.location),
      });
      promise
          .then(_ => {
              console.log('message added');
              this.message = {} as message;

          } )



}


}

This is how I generate profile:

import { Component,ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {AngularFireAuth} from 'angularfire2/auth';
import {AngularFireDatabase} from 'angularfire2/database';
import { TabsPage } from '../tabs/tabs';
//import { Profile } from '../../models/profile.ts';

@IonicPage()
@Component({
  selector: 'page-fp',
  templateUrl: 'fp.html',
})
export class FpPage {

//profile = {} as Profile
username: string
msgnumber: number
level: number



  constructor(private fire:AngularFireAuth,private db :AngularFireDatabase,public navCtrl: NavController, public navParams: NavParams) {
  }

  createProfile(){

this.fire.authState.take(1).subscribe(auth => {

this.db.object(`profile/${auth.uid}`).set({
username: this.username,
msgnumber: 0,
level: 0





}).then(() => this.navCtrl.setRoot(TabsPage));





})


  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FpPage');
  }

}

thanks


#2

there is $key?: string top of model file by the way.