【发布时间】:2021-03-23 17:15:43
【问题描述】:
我正在使用 Angular 11 和 firebase firestore。
我想获取集合中单个文档的文档 ID,以便我可以在名为 schedules 的子集合中创建一个子集合。
在 schedule.service.ts 我尝试使用以下指南,但没有成功:https://github.com/angular/angularfire/blob/master/docs/firestore/collections.md
更新: 我现在正在获取完整的数据列表 + 文档的 ID。
如何使用 UID(用户 ID)查询以查找我的文档并创建一个名为 schedule 的子集合?
schedule.service.ts
import {Injectable} from '@angular/core';
import {AngularFirestore} from '@angular/fire/firestore';
import {Schedule} from '../../model/schedule';
import {map} from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class ScheduleService {
constructor(public fireService: AngularFirestore) {
}
createSchedule(schedulecontent: Schedule) {
// prints
console.log('schedule', schedulecontent);
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;
console.log('id', id, 'data', data);
return {id, data};
});
})).subscribe();
// this only gets the data in the doc
// const res = this.fireService.collection('companies', ref =>
// ref.where('UID', '==', this.authService.currentUserId)).valueChanges().subscribe(data => {
// console.log('data', data[0]);
// });
//
// console.log('test', res);
// return res;
// return this.fireService.collection('companies').doc().collection('schedules').add(schedulecontent);
}
}
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';
@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
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', this.dateModel, '00:00');
}
// 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);
}
}
}
【问题讨论】:
-
snapshotChanges() 返回一个 Observable。您需要订阅该 Observable 才能执行管道中的任何内容。见rxjs-dev.firebaseapp.com/guide/observable
-
是的,现在我正在获取我所有的文档 ID 和数据。我如何通过使用文档 UID 值来选择我想要的那个?
-
如果您有可用的 firebase uid,您可以像这样查询该文档:github.com/angular/angularfire/blob/master/docs/firestore/…
标签: angular typescript google-cloud-firestore angularfire2