Here are a few documents I am fetching from firestore:
https://i.sstatic.net/YS2g4.png
This is the method I'm currently using:
private _mechanics = new BehaviorSubject<User[]>([]);
fetchMechanics() {
return of(
firebase.firestore().collection("users").where("isMechanic", "==", false)
.get()
.then((docs) => {
const data = []
docs.forEach((doc) => {
data.push(doc);
});
console.log('Service Data:', data);
this._mechanics.next(data)
}).catch((err) => {
console.log(err);
})
);
}
Now, I aim to transform the data to comply with my UserData
interface:
interface UserData {
userId: string,
userName: string,
isMechanic: boolean,
location: PlaceLocation
}
Could someone kindly advise on what modifications I should make to fetchMechanics()
for this purpose?
Here's an illustration of the data in firestore:
https://i.sstatic.net/DQmFW.png
In addition, I've been attempting to replicate the following code, but have not succeeded thus far:
private _users = new BehaviorSubject<User[]>([]);
fetchUsers() {
return this.http
.get<{ [key: string]: UserData }>('firebaseUrl/users.json')
.pipe(map(resData => {
const users = [];
for (const key in resData) {
if (resData.hasOwnProperty(key)) {
users.push(
new User(
key,
resData[key].userName,
resData[key].isMechanic,
resData[key].location)
);
}
}
return users;
}),
tap(users => {
this._users.next(users);
})
);
}