简体   繁体   中英

Saving additional fields on sign-up in Firebase

We're trying to work out why our information is not sending to our DB, except for email. We want to save name, surname, etc. to Firebase, but it is only saving email. What are we doing wrong?

signup.ts:

import { Component } from '@angular/core';
import { SearchPage } from '../search/search';
import { MyBookingsPage } from '../my-bookings/my-bookings';
import { NavController, AlertController, LoadingController, Loading } from 'ionic-angular';
import { Auth, User, UserDetails, IDetailedError } from '@ionic/cloud-angular';

import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AuthProvider } from '../../providers/auth/auth';
import { EmailValidator } from '../../validators/email';

@Component({
  selector: 'page-signup',
  templateUrl: 'signup.html'
})
export class SignupPage {
  public signupForm:FormGroup;
  public loading:Loading;
  constructor(public navCtrl: NavController, public authProvider:AuthProvider, public formBuilder: FormBuilder, public user: User, public alertCtrl: AlertController, public loadingCtrl:LoadingController) {

  this.signupForm = formBuilder.group({
    name: ['', Validators.compose([Validators.required])],
    surname: ['', Validators.compose([Validators.required])],
    birthday: ['', Validators.compose([Validators.required])],
    licenseNum: [''],
    email: ['', Validators.compose([Validators.required, EmailValidator.isValid])],
    password: ['', Validators.compose([Validators.minLength(6), Validators.required])]

  });

  }
  signupUser(){
    if (!this.signupForm.valid){
      console.log(this.signupForm.value);
    } else {
      this.authProvider.signupUser(this.signupForm.value.name,
        this.signupForm.value.surname,
        this.signupForm.value.birthday,
        this.signupForm.value.licenseNum,
        this.signupForm.value.email,
        this.signupForm.value.password)
      .then(() => {
        this.loading.dismiss().then( () => {
          this.navCtrl.setRoot(MyBookingsPage);
        });
      }, (error) => {
        this.loading.dismiss().then( () => {
          let alert = this.alertCtrl.create({
            message: error.message,
            buttons: [
              {
                text: "Ok",
                role: 'cancel'
              }
            ]
          });
          alert.present();
        });
      });
      this.loading = this.loadingCtrl.create();
      this.loading.present();
    }
  }

}

auth.ts:

@Injectable()
export class AuthProvider {

  public fireAuth:firebase.auth.Auth;
  public userProfileRef:firebase.database.Reference;

  constructor(public http: Http) {
    this.fireAuth = firebase.auth();
    this.userProfileRef = firebase.database().ref('/userProfile'); //linked to firebase node userProfile
    console.log('Hello AuthProvider Provider');
  }

  signupUser(name: string, surname: string, birthday: any, licenseNum: string, email: string, password: string ): firebase.Promise<any> {
    return this.fireAuth.createUserWithEmailAndPassword(email, password).then( newUser => {
      this.userProfileRef.child(newUser.uid).set({
        name: name,
        surname: surname,
        birthday: birthday,
        licenseNum: licenseNum,
        email: email,
        password: password
      });
    });
  }
}

This is the Firebase result.

We think that the problem is in signupUser in auth.ts, but we really don't know, and we can't find anything online that fits with our structure. If possible, we don't want to have to redo our entire project!

Thanks in advance!

您应该使用.update()函数而不是.set()函数来存储firebase数据库中的信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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