当我尝试从火库,火库获取指定用户时遇到问题。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我也有获取所有用户和添加新用户的功能,所以如果这是相关的,请在评论中告诉我
1 回答
![?](http://img1.sycdn.imooc.com/54584de700017cbd02200220-100-100.jpg)
白猪掌柜的
TA贡献1893条经验 获得超10个赞
您的代码行
this.id = this.route.snapshot.params.id;
在这种情况下,不是表格列,而是您的文档ID由Firestoreid
所以在这种情况下,你的是红色的,而不是蓝色的。Id
添加回答
举报
0/150
提交
取消