我正在尝试从 firestore 获取数据,然后将数据转换为数组。新数组应该是可观察的
I am trying to fetch data from firestore and then convert the data into an array. the new array should be an observable
我有一个服务,我在其中编写了一个从 friestore 获取数据的方法。
该方法需要 return 一个可观察对象。
然后数据被转换并存储在另一个变量中。
我想创建一个更新变量的可观察对象。我该怎么办。
我已经尝试使用 Subjects、ReplaySubjects 和 observables 创建观察者,但我想我遗漏了一些东西。
但是我得到了这个错误:
core.js:5847 ERROR Error: Uncaught (in promise): TypeError: Cannot
read property 'asObservable' of undefined
和
core.js:5847 ERROR TypeError: Cannot read property 'next' of undefined
the following is the code.
import { BehaviorSubject, Observable, of, Subscriber, ReplaySubject, Subject} from 'rxjs';
export class ProductsService {
public List : Product[] = [];
public productListObservable : Subject<Product[]>;
private products(): Observable<Product[]> {
//returns an observable of type Product[]
this.firestoreService.getProductsList().subscribe((doc) =>{
//Fetch data from firestore and
//create the new list with updated id's
this.List = doc.docs.map(prodItem =>{
let x = prodItem.data()
let test = {};
test['id']=prodItem.id;
test['name'] = x['ProductName'];
test['category'] = x['ProductCategory'];
test['tags']= x['ProductTags'];
test['specs'] = x['ProductSpecs'];
test['inclusions'] = x['ProductInclusion'];
test['shortDetails'] = x['ProductShortDetails'];
test['description'] = x['ProductDescription'];
test['pictures'] = x['ProductImages'];
return test;
})
this.productListObservable.next(this.List);
})
return this.productListObservable.asObservable();
}
public getProducts(): Observable<Product[]> {
//var test = this.products();
//test.subscribe(x =>{
// console.log(x);
//})
return this.products();
}
在Component.html
ngOnInit() {
this.productsService.getProducts().subscribe(product => this.products = product);
}
你得到的错误是因为你从未真正创建过你的主题对象。你只是声明了它。你可以改变
public productListObservable : Subject<Product[]>;
到
public productListObservable = new Subject<Product[]>();
或者,问题的措辞方式,听起来您甚至根本不需要这个主题。如果你只是将它用于 return 一个 Observable,你可以在你现有的 Observable 上使用 rxjs 的 map。类似于:
private products(): Observable<Product[]> {
//returns an observable of type Product[]
return this.firestoreService.getProductsList().pipe(map(doc => {
//Fetch data from firestore and
//create the new list with updated id's
this.List = doc.docs.map(prodItem =>{
let x = prodItem.data()
let test = {};
test['id']=prodItem.id;
test['name'] = x['ProductName'];
test['category'] = x['ProductCategory'];
test['tags']= x['ProductTags'];
test['specs'] = x['ProductSpecs'];
test['inclusions'] = x['ProductInclusion'];
test['shortDetails'] = x['ProductShortDetails'];
test['description'] = x['ProductDescription'];
test['pictures'] = x['ProductImages'];
return test;
});
return this.List;
});
}
我有一个服务,我在其中编写了一个从 friestore 获取数据的方法。
该方法需要 return 一个可观察对象。 然后数据被转换并存储在另一个变量中。 我想创建一个更新变量的可观察对象。我该怎么办。
我已经尝试使用 Subjects、ReplaySubjects 和 observables 创建观察者,但我想我遗漏了一些东西。 但是我得到了这个错误:
core.js:5847 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'asObservable' of undefined
和
core.js:5847 ERROR TypeError: Cannot read property 'next' of undefined the following is the code.
import { BehaviorSubject, Observable, of, Subscriber, ReplaySubject, Subject} from 'rxjs';
export class ProductsService {
public List : Product[] = [];
public productListObservable : Subject<Product[]>;
private products(): Observable<Product[]> {
//returns an observable of type Product[]
this.firestoreService.getProductsList().subscribe((doc) =>{
//Fetch data from firestore and
//create the new list with updated id's
this.List = doc.docs.map(prodItem =>{
let x = prodItem.data()
let test = {};
test['id']=prodItem.id;
test['name'] = x['ProductName'];
test['category'] = x['ProductCategory'];
test['tags']= x['ProductTags'];
test['specs'] = x['ProductSpecs'];
test['inclusions'] = x['ProductInclusion'];
test['shortDetails'] = x['ProductShortDetails'];
test['description'] = x['ProductDescription'];
test['pictures'] = x['ProductImages'];
return test;
})
this.productListObservable.next(this.List);
})
return this.productListObservable.asObservable();
}
public getProducts(): Observable<Product[]> {
//var test = this.products();
//test.subscribe(x =>{
// console.log(x);
//})
return this.products();
}
在Component.html
ngOnInit() {
this.productsService.getProducts().subscribe(product => this.products = product);
}
你得到的错误是因为你从未真正创建过你的主题对象。你只是声明了它。你可以改变
public productListObservable : Subject<Product[]>;
到
public productListObservable = new Subject<Product[]>();
或者,问题的措辞方式,听起来您甚至根本不需要这个主题。如果你只是将它用于 return 一个 Observable,你可以在你现有的 Observable 上使用 rxjs 的 map。类似于:
private products(): Observable<Product[]> {
//returns an observable of type Product[]
return this.firestoreService.getProductsList().pipe(map(doc => {
//Fetch data from firestore and
//create the new list with updated id's
this.List = doc.docs.map(prodItem =>{
let x = prodItem.data()
let test = {};
test['id']=prodItem.id;
test['name'] = x['ProductName'];
test['category'] = x['ProductCategory'];
test['tags']= x['ProductTags'];
test['specs'] = x['ProductSpecs'];
test['inclusions'] = x['ProductInclusion'];
test['shortDetails'] = x['ProductShortDetails'];
test['description'] = x['ProductDescription'];
test['pictures'] = x['ProductImages'];
return test;
});
return this.List;
});
}