Tomasz
Tomasz

Reputation: 210

Post data to Firebase from Angular 4

I use the Angular 4 and AngularFire to send data to Firebase.

I need to get a few data like: name, country, zip-code with email and password.

After update: //

The HTML FORM :

<form #formData='ngForm' (ngSubmit)="onSubmit(formData)">
 <input type="text" placeholder="Name" (ngModel)="name" name="name" class="txt" required>
 <input type="text" placeholder="Country" (ngModel)="country" name="country" class="txt" required>
 <input type="text" placeholder="code" (ngModel)="code" name="code" class="txt" required>
 <input type="text" placeholder="Email addres" (ngModel)="email" name="email" class="txt" required>
 <input type="password" placeholder="Password" (ngModel)="password" name="password" class="txt" required>
 <button type="submit" [disabled]="!formData.valid" class="basic-btn">Create Account</button>
</form>

I use the model to get value and use the code to send it:

constructor(private router: Router, private db: AngularFireDatabase) {}

onSubmit(formData) {
 if(formData.valid) {
 console.log(formData.value);
 this.db.auth.createUser({
  email: formData.value.email,
  password: formData.value.password
 }).then (
   (state: AngularFireAuth) => {
    this.db.object('/users/' + state.uid).set({code: formData.value.code});
    this.router.navigate(['/login'])
  }).catch(
    (err) => {
      console.log(err);
      this.error = err;
    })
 }
}

And I get error in: The datebase don't have auth, FireAuth also don't have createUser

this.db.auth.createUser

And at this there is no uid property

this.db.object('/users/' + state.uid).set({code: formData.value.code});

Upvotes: 0

Views: 2071

Answers (1)

Yevgen
Yevgen

Reputation: 4709

There is no way to save extra data in the auth part of Firebase. You can save all extra information in database itself under user with same uid as in authentication part.

constructor(private db: AngularFireDatabase) {}

onSubmit(formData) {
 if(formData.valid) {
  console.log(formData.value);
  this.af.auth.createUser({
    email: formData.value.email,
    password: formData.value.password
  }).then (
    (state: FirebaseAuthState) => {
      db.object('/users/' + state.uid).set({zip: formData.value.zip});
      this.router.navigate(['/login'])
    }).catch(
      (err) => {
        console.log(err);
        this.error = err;
      })
   }
}

Upvotes: 4

Related Questions