【发布时间】:2021-03-24 12:29:10
【问题描述】:
我正在尝试在我的计划子集合中获取数据,以便我可以在我的 html 模板中显示它。
到目前为止,我在 schedule.service.ts 中的 getScheduleList 方法中得到了什么。看来无论我尝试做什么,我都只能获取元数据。
我尝试使用:https://github.com/angular/angularfire 获取文档,因为 firebases 自己的有点缺乏。
schedule.service.ts
import {Injectable} from '@angular/core';
import {AngularFirestore} from '@angular/fire/firestore';
import {Schedule} from '../../model/schedule';
import {map} from 'rxjs/operators';
import {AuthService} from '../../auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class ScheduleService {
constructor(public fireService: AngularFirestore, private authService: AuthService) {
}
getScheduleList() {
this.fireService.collection<any>('companies').snapshotChanges().pipe(map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
if (data.UID === this.authService.currentUserId) {
console.log(id);
this.fireService.collection(`companies/${id}/schedules`).snapshotChanges().subscribe(result => {
console.log('test', result);
});
}
});
})).subscribe();
}
}
schedule.component.ts
import {Component, OnInit} from '@angular/core';
import {NgbModal, NgbDateStruct, NgbCalendar, NgbModalConfig} from '@ng-bootstrap/ng-bootstrap';
import {NgForm} from '@angular/forms';
import {Schedule, ScheduleInterface} from '../../model/schedule';
import {ScheduleService} from '../../service/schedule/schedule.service';
import {ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-schedule',
templateUrl: './schedule.component.html',
styleUrls: ['./schedule.component.css'],
})
export class ScheduleComponent implements OnInit {
dateModel: NgbDateStruct; // Holds the date structure day/month/year format
date: { year: number, month: number }; // Is for the datepicker month and year selector
schedule: ScheduleInterface; // schedule object uses interface from schedule models
scheduleList: ScheduleInterface[];
constructor(private config: NgbModalConfig,
private modalService: NgbModal,
private calendar: NgbCalendar,
private serviceSchedule: ScheduleService) {
// Customize default values of modals used by this component tree
config.backdrop = 'static';
config.keyboard = false;
}
// Initialises the schedule object with default values
ngOnInit(): void {
// this.schedule = new Schedule('', 'default', '10', '00:00');
this.schedule = new Schedule('', 'default', '2020', '00:00');
this.getScheduleList();
}
// Opens modal window
open(content) {
this.modalService.open(content);
}
// Gives to current day in the datepicker
selectToday() {
this.dateModel = this.calendar.getToday();
}
// Creates new task for the schedule
createSchedule(schedulecontent: NgForm) {
console.log(schedulecontent.value);
if (schedulecontent.valid) {
this.serviceSchedule.createSchedule(schedulecontent.value);
}
}
getScheduleList() {
const test = this.serviceSchedule.getScheduleList();
// console.log(test);
}
}
【问题讨论】:
标签: javascript angular typescript google-cloud-firestore collections