'Firestore / Angular / Typescript - get value to global variable from subscription of firestore collection
How can I save the value depends from subscribe for use it in global scope and other class. I have service in angular:
public maxId: any;
constructor(private db: AngularFirestore) {
this.miejscowosciRef = db.collection('miejscowosci', ref => ref.orderBy('id', 'desc'));
}
getMaxId() {
this.miejscowosciRef.valueChanges({idField: 'id'})
.subscribe(data => {
this.maxId = data.reduce((prev, current) => (prev.id > current.id) ? prev : current).id;
console.log('test 1: ' + this.maxId) // ok.. value is here
})
console.log('test 2: ' + this.maxId); // no.. value outside subscribe scope shows undefined
}
I know why it's happen that value is undefined but I have no idea how to save it to global variable or return to other variable.
I want to generate next my own ID for firestore document so I need to know what is value of last one. Maybe there is some other methods for generate next one? I have my own ID: 10001, 10002, 10003 etc. So I have to know the last is 10003 before add new item.
Solution 1:[1]
I don't love the idea of self-managed IDs. Ordinarily it is best to let the database (Firestore in this case) handle the creation IDs for documents, as it is less likely to make mistakes (re-using IDs). That said, here's an answer to your question.
By using Firestore's subscribe method, you are establishing an anonymous listener method to run on every value change. When you call getMaxId, you are setting up that listener.
But from the code you have written, it looks like what you want is to return the maxId from Firestore. You can get your desired behavior using either method, but you've got to pick one.
If I were you, I would use your subscription and this.maxId to refer to the current maximum ID number, which will always be up to date thanks to the listener.
subscribeMaxIdListener() {
this.miejscowosciRef.valueChanges({ idField: 'id' }).subscribe(data => {
this.maxId = data.reduce((prev, current) => (prev.id > current.id) ? prev : current).id;
console.log('New Max ID: ' + this.maxId) // Fires with every value change event
});
console.log('Listener Established'); // You should see this only once
}
You should call subscribeMaxIdListener only once! From then on, this.maxId will always have the value you are looking for.
One caveat: if you try to get this.maxId before Firestore returns a value to the listener, it will of course be undefined. As such, you will want to be careful about when you use it and fail gracefully if Firestore hasn't responded yet.
Solution 2:[2]
ArticleName: any;
getNameById(getId):any{
this.towaryRef.get()
.subscribe((ss) => {
ss.docs.forEach((doc) => {
if (doc.id == getId) {
this.ArticleName = doc.data().name
}
});
});
}
And in other class I read from service:
this.fsTowaryService.getNameById('10004');
myNazwa = this.fsTowaryService.ArticleName ;
I also tried with return function and still the same. I have right data on service but no idea how to save it to global variable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Yamik |
