简体   繁体   English

(Angular / Firestore)抓取/保存集合到变量

[英](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.

相关问题 Firestore / Angular / Typescript - 从 firestore 集合的订阅中获取全局变量的值 - Firestore / Angular / Typescript - get value to global variable from subscription of firestore collection Firestore Angular 文档中的集合 - Firestore Angular Collection inside Document Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中? - Firebase Firestore iOS: How can I save images to a collection in the firestore? 如何将 Firestore 编号保存到 flutter 中的变量中? - How to save a Firestore number into a variable in flutter? Swift - 将 firestore 集合的所有文档保存在对象列表中 - Swift - Save all documents of firestore collection in a list of objects Angular Firestore - 获取文档数据并分配给变量 - Angular Firestore - Get document data and assign to variable Angular Firestore Group collection 获取多个文档下的所有文档 - Angular Firestore Group collection get all docs under a multiple document 如何使用angular firestore获取集合中的文档列表 - how to get list of document in a collection, using angular firestore Angular Firestore:获取集合时 Map DocumentReference 属性为 object - Angular Firestore: Map DocumentReference attribute to object while fetching collection 使用 Angular (@angular/fire) 在 firestore 中添加、更新和读取子集合 - Add, update and read sub-collection in firestore using Angular (@angular/fire)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM