0
votes

So I am relatively new to ionic and i am trying to add a user to the realtime firebase database. The user needs to have more details than just a email and password(for example their first name, last name, address, phone number etc) as i need the details later on.

I am following a tutorial and i believe due to the tutorial being slightly old I am unable to get the function work due to this error:

Error: Uncaught (in promise): Error: Reference.child failed: First argument was an invalid path = "profile/${auth.uid}". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]" Error: Reference.child failed: First argument was an invalid path = "profile/${auth.uid}". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

I am not entirely sure what am i doing wrong so any help would be appreciated. My code is below

Profile.ts 


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

/**
 * Generated class for the ProfilePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

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

  profile = {} as Profile

  constructor(private afAuth:AngularFireAuth, private afDatabase: AngularFireDatabase, public navCtrl: NavController, public navParams: NavParams) {
  }



  createProfile()  {
    this.afAuth.authState.take(1).subscribe(auth => {
      this.afDatabase.object('profile/${auth.uid}').set(this.profile)
      .then(() => this.navCtrl.setRoot(TabsPage));

    })
  }

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

}

Profile.html

<ion-header>

  <ion-navbar>
    <ion-title>Profile</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
    <ion-item>
        <ion-label floating>Username</ion-label>
        <ion-input [(ngModel)]="profile.username" name="username"></ion-input>
      </ion-item>
      <ion-item>
          <ion-label floating>Username</ion-label>
          <ion-input [(ngModel)]="profile.firstname" name="firstname"></ion-input>
        </ion-item>

      <div>
          <button ion-button (click)= "createProfile()" full color='dark'> Create</button>
        </div>


</ion-content>

App.component.ts

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { FormsModule } from '@angular/forms';
import{FIREBASE_CONFIG}  from './app.firebase.config'

import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import {LoginPage} from '../pages/login/login';
import {ProfilePage} from '../pages/profile/profile';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { RegisterPage } from '../pages/register/register';
import {AngularFireModule} from 'angularfire2';
import {AngularFireAuthModule} from 'angularfire2/auth';
import {AngularFireDatabase, AngularFireDatabaseModule} from 'angularfire2/database';


@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    LoginPage,
    RegisterPage,
    ProfilePage

  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    AngularFireModule,
    AngularFireModule.initializeApp(firebaseAuth),
    AngularFireAuthModule,
    AngularFireDatabaseModule,
    FormsModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    LoginPage,
    RegisterPage,
    ProfilePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}
1

1 Answers

0
votes
If you pass variable inside the path like this ${uid}, 

you can not use apostrophe ('profile/${auth.uid}'), so You need to use grave accent (``) (profile/${auth.uid}) for that. Also you can use this way ("profile/"+auth.uid)

createProfile()  {
  this.afAuth.authState.take(1).subscribe(auth => {
    this.afDatabase.object(`profile/${auth.uid}`).set(this.profile)
     .then(() => this.navCtrl.setRoot(TabsPage));

  })
}

try this one also,

createProfile()  {
  this.afAuth.authState.take(1).subscribe(auth => {
    this.afDatabase.database.ref('/profile').child(auth.uid)
    .set(this.profile).then(() => this.navCtrl.setRoot(TabsPage));

  })
}