1
votes

Hello im sitting here with a project i cannot find the error or what iam doing wrong.

Iam using angular 6 and firebase/firestore

My problem is when i want to create a user with email and password it works fine but i then added more fields like firstname, lastname and so on.

Auth.service.ts

My auth service have the interface and the to functions showed below here. i tested the creating user with only email and password and that worked fine.

in my firebase i have activated the auth with email/password and i created a collection users with a document users and added all the fields firstName, lastName and so on.

interface User {
uid: string;
email?: string;
photoURL?: string;
displayName?: string;
firstName?: string;
lastName?: string;
address?: string;
zipCode?: string;
city?: string;
phoneNumber?: string;

}


emailSignUp(email: string, password: string) {
return this.afAuth.auth
  .createUserWithEmailAndPassword(email, password)
  .then(credential => {
    this.notify.update('Welcome new user!', 'success');
    return this.updateUserData(credential.user); // if using firestore
  })
  .catch(error => this.handleError(error));

}

  private updateUserData(user) {
// Sets user data to firestore on login

const userRef: AngularFirestoreDocument<any> = 
this.afs.doc(`users/${user.uid}`);

const data: User = {
  uid: user.uid,
  email: user.email,
  displayName: user.displayName,
  photoURL: user.photoURL,
  firstName: user.firstName,
  lastName: user.lastName,
  address: user.address,
  zipCode: user.zipCode,
  city: user.city,
  phoneNumber: user.phoneNumber
}

return userRef.set(data, { merge: true })

}

 User-form.component.html

This is my component where i have my form for creating a new user.

 <form [formGroup]="userForm" *ngIf="newUser" (ngSubmit)="signup()">
    <h1>Log ind / Opret ny profil</h1>
    <br>

<h3>Har du allerede en bruger så tryk på log ind</h3>
<p class="btn btn-primary" (click)="toggleForm()">Log ind</p>

<h3>Opret ny profil</h3>
<!--<p class="button is-small" (click)="toggleForm()">Already Registered? 
</p>-->
<hr>

<label for="firstName">Fornavn</label>
<input type="text" class="input" formControlName="firstName" 
name="firstName" required >

<label for="lastName">Efternavn</label>
<input type="text" class="input" formControlName="lastName" name="lastName" 
required>

<label for="telefonnummer">Telefonnummer</label>
<input type="tel" class="input" formControlName="phoneNumber" 
name="telefonnummer" required>

<label for="adresse">Adresse</label>
<input type="text" class="input" formControlName="address" name="adresse" 
required>

<label for="postnummer">Postnummer</label>
<input type="number" class="input" formControlName="zipCode" 
name="postnummer" required>

<label for="by">By</label>
<input type="text" class="input" formControlName="city" name="by" required>

<label for="email">Email</label>
<input type="email" class="input" formControlName="email" name="email" 
required autocomplete="new-password">

<div *ngIf="formErrors.email" class="notification is-danger">
    {{ formErrors.email }}
</div>

<label for="password">Password</label>
<input type="password" class="input" formControlName="password" 
name="password" required>

<div *ngIf="formErrors.password" class="notification is-danger">
    {{ formErrors.password }}
</div>

<!--
<label>
    <input type="checkbox" class="checkbox" required>Service & Betingelser
</label>
-->

<div *ngIf="userForm.valid" class="notification is-success">Form is 
valid</div>
<button type="submit" class="btn btn-primary" 
[disabled]="!userForm.valid">Opret Profil</button>

</form>
1
In updateUserData() method, console.log to see what user looks like. Is firstName there? - rrd
I have tryed to remove most of the input fields to test with only 1 field (more easy to check) But i get a null in my displayname field {uid: "rpa4lSZb9Wam1uw7lwu3fwdclLR2", email: "[email protected]", displayName: null} displayName: null email: "[email protected]" uid: "rpa4lSZb9Wam1uw7lwu3fwdclLR2" - Niclas Krabbe Rosenby

1 Answers

0
votes

Now, since firestore has upadated its API in its 5th Version. So, replace user by cred.