當我嘗試從火庫,火庫獲取指定用戶時遇到問題。export class TaskService { tasksCollection: AngularFirestoreCollection<Task>; taskDoc: AngularFirestoreDocument<Task>; tasks: Observable<Task[]>; task: Observable<Task>; constructor(private afs: AngularFirestore) { this.tasksCollection = this.afs.collection('tasks', ref => ref.orderBy('title', 'asc')); } getTask(id: string): Observable<Task> { this.taskDoc = this.afs.doc<Task>(`clients/${id}`); this.task = this.taskDoc.snapshotChanges().pipe(map(action => { if (action.payload.exists === false) { return null; } else { const data = action.payload.data() as Task; data.id = action.payload.id; return data; } })); return this.task; }}這是我的文件Component.tsexport class TaskDetailsComponent implements OnInit { id: string; task: Task; hasHours = false; showHoursOnUpdate: false; constructor( private taskService: TaskService, private router: Router, private route: ActivatedRoute ) { } ngOnInit() { // Get id from url this.id = this.route.snapshot.params.id; // Get client this.taskService.getTask(this.id).subscribe(task => { if (task != null) { if (task.hours > 0) { this.hasHours = true; } } this.task = task; }); console.log(this.id); console.log(this.task); }}id 的結果是好的。但對象(任務)的結果未定義。P.S我也有獲取所有用戶和添加新用戶的功能,所以如果這是相關的,請在評論中告訴我
嘗試獲取對象時無法設置未定義的屬性“id”
料青山看我應如是
2022-09-23 16:58:40