Kyrill
Kyrill

Reputation: 334

Type 'Observable' is missing the following properties from type 'User'

i want to get user data from firestore, but i get the error

Type 'Observable' is missing the following properties from type 'User': email, emailVerified, uidts(2739)

user.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { User } from '../_classes/user';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private dbPath = '/users';
  usersRef: AngularFirestoreCollection<User>;
  userData: User;

  constructor(private db: AngularFirestore) {
    this.usersRef = db.collection(this.dbPath);
    this.getUserData();
  }

  private getUserData():User {
    const userId = JSON.parse(localStorage.getItem('currentUser')).uid;

    ** Here comes the error **
    this.userData = this.usersRef.doc(userId).valueChanges();
  }

  get UserData():User {
    return this.userData;
  }
}

user.ts

export class User {
email: string;
emailVerified: boolean;
firstName?: string;
lastName?: string;
mitarbeiter?: boolean;
phone?: string;
uid: string;

}

firebase firestore user example

Upvotes: 2

Views: 3493

Answers (1)

TotallyNewb
TotallyNewb

Reputation: 4790

I never used it myself, but according to AngularFirestore docs valueChanges() returns an observable, not a value. Hence you get the error about type mismatch - you can't assign Observable to variable of type user. Instead, you need to either subscribe to the observable or operate on the observable itself - so it could be something along this way:

this.usersRef.doc(userId).valueChanges().subscribe(v => {
  this.userData = v;
});

Upvotes: 2

Related Questions