user9847788
user9847788

Reputation: 2435

How to convert Query Snapshot from Firebase into a different object?

In the below method, I am retrieving a document from a Firebase collection.

I have managed to log the values I need to return when getUserByUserId() is called, but I need to return them as a User object:

getUserByUserId(userId: string) {
    return of(firebase.firestore().collection("users").where("userId", "==", userId)
      .get().then(querySnapshot => {
        querySnapshot.forEach(doc => {
          console.log("User ID", "=>", doc.data().userId);
          console.log("Username", "=>", doc.data().userName);
          console.log("Mechanic", "=>", doc.data().isMechanic);
          console.log("Location", "=>", doc.data().location);
        })
      }).catch(err => {
        console.log(err);
      }));
  }

Here is the User structure that the data will need to follow:

import { PlaceLocation } from './location.model';

export class User {
    constructor(
        public id: string,
        public name: string,
        public isMechanic: boolean,
        public email: string,
        public location: PlaceLocation
    ) { }
}

Can someone please tell me how I can create a User object with this data & return it as the response of getUserByUserId()?

Upvotes: 3

Views: 1437

Answers (1)

Radik
Radik

Reputation: 2795

with @angular/fire you can do as follow

constructor(private firestore: AngularFirestore) {
}

getUserByUserId(userId: string) {

    return this.firestore
      .collection("users", ref => ref.where("userId", "==", userId))
      .get()
      .pipe(
        filter(ref => !ref.empty),
        map(ref => ref.docs[0].data() as User),
        map(data => new User(data, data.location))
      )

}

updated

if you need object instance you should have additional constructor like this about object assign

export class User {
    constructor(
        public id: string,
        public name: string,
        public contactNumber: number,
        public isMechanic: boolean,
        public email: string,
        public password: string,
        public address: string,
        public imageUrl: string,
        public location: PlaceLocation
    ) { }

    public constructor(
      init?: Partial<User>,
      initLocation?: Partial<PlaceLocation>) {

        Object.assign(this, init);
        if(initLocation) {
          this.location = new PlaceLocation(initLocation);
        }
    }
}

export class PlaceLocation {
    constructor() { }

    public constructor(init?: Partial<PlaceLocation>) {
        Object.assign(this, init);
    }
}

because you read data as object without type you can only create a new User object explicitly and assign it properties using data from object

getUserByUserId(userId: string) {

    return this.firestore
      .collection("users", ref => ref.where("userId", "==", userId))
      .get()
      .pipe(
        filter(ref => !ref.empty),
        map(ref => ref.docs[0].data() as User),
        map(data => new User(data, data.location))
      )

}

Upvotes: 5

Related Questions