【发布时间】:2021-03-22 08:08:00
【问题描述】:
已经有一段时间了。 我的问题是如何通过当前登录的用户 ID 将数据存储在实时数据库(firebase)中,所以当我从另一个帐户登录时,我看不到该数据(只有我自己的)。 这就是我现在的做法: employee.service.ts:
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
userId: string;
constructor(public firebase: AngularFireDatabase, private datePipe: DatePipe, private afu:
AngularFireAuth, public clientService: ClientService, public contractsService: ContractsService,
public maintainerService: MaintainerService) {
this.afu.authState.subscribe(user=>{
if(user) this.userId=user.uid;
})
}
employeeList: AngularFireList<any>;
clientList: AngularFireList<any>;
maintainerList: AngularFireList<any>;
contractList: AngularFireList<any>;
array=[];
form: FormGroup=new FormGroup({
$key: new FormControl(null),
sifra: new FormControl(''),
caseName: new FormControl(''),
department: new FormControl(''),
startDate: new FormControl(new Date()),
startTime: new FormControl(''),
finishTime: new FormControl(''),
isPermanent: new FormControl(false), //nije obavezno
description: new FormControl(''),
remark: new FormControl(''), //nije obavezno
serviceType: new FormControl('1'),
isReplaceable: new FormControl(''),
maintainer: new FormControl(''),
contracts: new FormControl(''),
dnevnica: new FormControl(''),
client: new FormControl('')
});
initializeFormGroup(){
this.form.setValue({
$key: null,
sifra: '',
caseName: '',
department: '',
startDate: '',
startTime: '',
finishTime: '',
isPermanent: false,
description: '',
remark: '',
serviceType: '1',
isReplaceable: '',
maintainer: '',
contracts: '',
dnevnica: '',
client: ''
});
}
getEmployees(){
this.employeeList=this.firebase.list(`employees/${this.userId}`);
return this.employeeList.snapshotChanges();
}
在我的组件文件中:
ngOnInit(): void {
this.service.getEmployees().subscribe(
list=>{
let array = list.map(item=>{
let clientName=this.clientService.getClientName(item.payload.val()['client']);
let maintainerName=this.maintainerService.getMaintainerName(item.payload.val()['maintainer']);
return{
$key: item.key,
clientName,
maintainerName,
...item.payload.val()
};
});
this.listData= new MatTableDataSource(array);
this.listData.sort=this.sort;
this.listData.paginator=this.paginator;
this.listData.filterPredicate=(data, filter)=>{
return this.displayColumns.some(ele=>{
return ele != 'actions' && data[ele].toLowerCase().indexOf(filter) != -1;
});
}
});
}
当我第一次登录时,一切都很好。当我刷新页面时,我的一切都消失了! 这很奇怪,因为我的数据仍在我的数据库中,但是如果我单击浏览器上的返回按钮并再次输入我的组件,数据又在那里! 提前致谢。
【问题讨论】:
标签: javascript angular firebase firebase-realtime-database