[英](Angular / Firestore) Grab / save collection to variable
I am trying to save collection from Firestore to an array.我正在尝试将 Firestore 中的集合保存到数组中。 I can show it in html list but no idea how to save it to a variable.
我可以在 html 列表中显示它,但不知道如何将它保存到变量中。 I know this is async It is working with *ngFor="let zasob of itemsZasoby" but still undefined in a variable.
我知道这是异步的,它正在与 *ngFor="let zasob of itemsZasoby" 一起工作,但在变量中仍未定义。 I need that for prepare json data to scheduler.
我需要它来为调度程序准备 json 数据。
I was trying many examples from inte.net and no success.我尝试了来自 inte.net 的许多示例,但都没有成功。 Maybe someone knows solution for async in angular to save the array?
也许有人知道 angular 中异步的解决方案来保存数组? Switchmap or so?
开关图左右? I am crying ritght now..
我现在在哭。。
Service class:客服class:
import { Injectable, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { FsZasobyModel } from '../models/fs-zasoby.model';
@Injectable({
providedIn: 'any'
})
export class FsZasobyService implements OnInit {
private dbPath = '/zasoby';
zasobyRef: AngularFirestoreCollection<FsZasobyModel>;
constructor(private db: AngularFirestore) {
this.zasobyRef = db.collection(this.dbPath);
}
ngOnInit(): void {
}
getAll(): AngularFirestoreCollection<FsZasobyModel> {
return this.zasobyRef;
}
}
Calendar class:日历 class:
export class KalendarzComponent implements AfterViewInit, OnDestroy {
itemsZasoby: any;
@ViewChild(BryntumSchedulerComponent, { static : true }) schedulerComponent: BryntumSchedulerComponent;
constructor(
private fsZasobyService: FsZasobyService) {}
ngOnInit(): void {
this.getItemsZasoby2();
}
getItemsZasoby2(filterValue) {
this.fsZasobyService.getAll()
.valueChanges().subscribe(data => {
this.itemsZasoby= data
console.log(this.itemsZasoby); -- There is data in an array
})
console.log(this.itemsZasoby); -- undefined cause of async
}
}
I'm not sure what you are trying to accomplish - could you elaborate on what you are trying to do?我不确定你想要完成什么 - 你能详细说明你想要做什么吗? anyway, the reason it is undefined is like you said - it's async.
无论如何,它未定义的原因就像你说的那样——它是异步的。 this means that you are calling the second console.log - before the data has arrived.
这意味着您正在调用第二个 console.log - 在数据到达之前。 that is why it's undefined.
这就是它未定义的原因。 whatever manipulation you want to do on the data needs to be done afterward.
您想对数据进行的任何操作都需要在之后完成。 if you use switchmap/map/flatmap or whatever map inside the call itself - it's still an async function.
如果您在调用本身内部使用 switchmap/map/flatmap 或任何 map - 它仍然是异步 function。
I have a config for scheduler what needs a json data:我有一个调度程序配置,需要 json 数据:
setKalendarzConfig() {
this.kalendarzConfig = {
resources : this.itemsZasoby,
events : this.itemsZamowienia,
barMargin : 10,
rowHeight : 60,
minHeight : 150,
eventStyle : 'border',
mode :'vertical',
}
}
In demo version it reads from json file with structure:在演示版本中,它从 json 文件中读取结构:
resources = [
{ id : '10001', name : 'kalKlasa 3GDA 07809', 'role': '[P]' },
{ id : '10005', name : 'G9 WOBET', 'role': '[PG]'},
]
So I am trying to get data from Firestore collection and save it to a variable this.itemsZasoby for replace date from file.所以我试图从 Firestore 集合中获取数据并将其保存到变量 this.itemsZasoby 以从文件中替换日期。 Then I can go to work with firebase insteand of a local file.
然后我可以 go 使用 firebase 而不是本地文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.