【问题标题】:How to retrieve subcollection documents - Angular Firebase如何检索子集合文档 - Angular Firebase
【发布时间】: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);
  }


}

firebase 集合

【问题讨论】:

    标签: javascript angular typescript google-cloud-firestore collections


    【解决方案1】:

    这几乎肯定不是你所期望的:

    this.fireService.collection('companies/{id}/schedules')
    

    如果你想在此处的字符串中使用id的值,你应该这样表达:

    this.fireService.collection(`companies/${id}/schedules`)
    

    注意反引号和美元符号。这是 JavaScript 使用 template literals 进行字符串插值的方式。

    【讨论】:

    • 我已经编辑但仍然得到相同的输出
    猜你喜欢
    • 1970-01-01
    • 2018-08-22
    • 2021-02-20
    • 1970-01-01
    • 2018-12-13
    • 2019-02-05
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多