如何从 observable 中正确获取数组数据?
How to properly get array data from observable?
我需要两个函数来 return 所有数据以及特定的过滤数据,但我的构造是错误的。下面是我想要的 "think",但是 return 订阅而不是数组:
allItems() {
var collectionAll: AngularFirestoreCollection<Item> =
this._afs.collection<Item>('items');
var itemArray$: Observable<Item[]> =
collectionAll.valueChanges();
// Returns Subscription but I need Items[]
return itemArray$.subscribe(items => {
return items;
})
}
specificItems(name: string) {
var collectionSpecific: AngularFirestoreCollection<Item> =
this._afs.collection<Item>('items', ref =>
ref.where('name', '==', name));
var itemArray$: Observable<Item[]> =
collectionSpecific.valueChanges();
// Returns Subscription but I need Items[]
return itemArray$.subscribe(items => {
return items;
})
}
另外我认为它需要是一个异步函数,但订阅函数并不是 return 一个承诺。
而且我什至不确定我实际上会在什么时候从 Firestore 中收取读取计数...?
如果你想要一个 promise,你需要使用 toPromise
:
将 Observable 转换为 Promise
specificItems(name: string): Promise<Item[]> {
var collectionSpecific: AngularFirestoreCollection<Item> =
this._afs.collection<Item>('items', ref =>
ref.where('name', '==', name));
var itemArray$: Observable<Item[]> =
collectionSpecific.valueChanges();
return itemArray$.toPromise();
}
Observables 非常强大,你应该保持原样。
allItems = this._afs.collection<Item>('items').valueChanges();
在您的模板中,您可以简单地使用异步管道来读取您的数据:
<div *ngFor="let items of allItems | async">...</div>
由于几个原因,这是使用 Angular 的最强大的方式,所以请尽快学习它,因为基本上 Angular = RxJS(当然不是真的,但它告诉你在 Angular)
中你需要多少 RxJS
Declare the below model in different location so that you can reuse the same model.
export class EmployeeRoster {
RosterDate: Date;
EmployeeId: number;
RosterDayName: string;
ProjectId: number;
ShiftId: number;
ShiftTime: string;
ShiftColor: string;
IsOnLeave: boolean;
}
Declare the below method in your service layer.
GetSavedEmployeeData(empIds: EmployeeIdlistToRoster[], sDate: string, eDate: string): Observable<EmployeeRoster[]> {
let empIdsValue = '';
if (empIds !== null && empIds.length > 0) {
empIds.forEach(function (em) {
empIdsValue += em.EmpId + ',';
});
}
//How to pass value as a parameter
const paramsdsf = new HttpParams()
.set('empIds', empIdsValue)
.append('sDate', sDate)
.append('eDate', eDate);
return this.http.get<EmployeeRoster[]>(apiUrl, { params: paramsdsf });
}
This is just an example you can update this method and model as per your requirement.
我需要两个函数来 return 所有数据以及特定的过滤数据,但我的构造是错误的。下面是我想要的 "think",但是 return 订阅而不是数组:
allItems() {
var collectionAll: AngularFirestoreCollection<Item> =
this._afs.collection<Item>('items');
var itemArray$: Observable<Item[]> =
collectionAll.valueChanges();
// Returns Subscription but I need Items[]
return itemArray$.subscribe(items => {
return items;
})
}
specificItems(name: string) {
var collectionSpecific: AngularFirestoreCollection<Item> =
this._afs.collection<Item>('items', ref =>
ref.where('name', '==', name));
var itemArray$: Observable<Item[]> =
collectionSpecific.valueChanges();
// Returns Subscription but I need Items[]
return itemArray$.subscribe(items => {
return items;
})
}
另外我认为它需要是一个异步函数,但订阅函数并不是 return 一个承诺。
而且我什至不确定我实际上会在什么时候从 Firestore 中收取读取计数...?
如果你想要一个 promise,你需要使用 toPromise
:
specificItems(name: string): Promise<Item[]> {
var collectionSpecific: AngularFirestoreCollection<Item> =
this._afs.collection<Item>('items', ref =>
ref.where('name', '==', name));
var itemArray$: Observable<Item[]> =
collectionSpecific.valueChanges();
return itemArray$.toPromise();
}
Observables 非常强大,你应该保持原样。
allItems = this._afs.collection<Item>('items').valueChanges();
在您的模板中,您可以简单地使用异步管道来读取您的数据:
<div *ngFor="let items of allItems | async">...</div>
由于几个原因,这是使用 Angular 的最强大的方式,所以请尽快学习它,因为基本上 Angular = RxJS(当然不是真的,但它告诉你在 Angular)
中你需要多少 RxJSDeclare the below model in different location so that you can reuse the same model.
export class EmployeeRoster {
RosterDate: Date;
EmployeeId: number;
RosterDayName: string;
ProjectId: number;
ShiftId: number;
ShiftTime: string;
ShiftColor: string;
IsOnLeave: boolean;
}
Declare the below method in your service layer.
GetSavedEmployeeData(empIds: EmployeeIdlistToRoster[], sDate: string, eDate: string): Observable<EmployeeRoster[]> {
let empIdsValue = '';
if (empIds !== null && empIds.length > 0) {
empIds.forEach(function (em) {
empIdsValue += em.EmpId + ',';
});
}
//How to pass value as a parameter
const paramsdsf = new HttpParams()
.set('empIds', empIdsValue)
.append('sDate', sDate)
.append('eDate', eDate);
return this.http.get<EmployeeRoster[]>(apiUrl, { params: paramsdsf });
}
This is just an example you can update this method and model as per your requirement.