我想从火力分贝使用snapshotChanges获得对象的列表。
Angular版本:7.2.0, 火力地堡版本:5.8.1, RxJS版本:6.3.3, AngularFire2:5.1.1
我的代码如下:
this.fbSubs.push(this.db
.collection('availableExercises')
.snapshotChanges()
.pipe(
map(docArray => {
return docArray.map(doc => {
return {
idExercise: doc.payload.doc.id,
name: doc.payload.doc.data().name,
duration: doc.payload.doc.data().duration,
calories: doc.payload.doc.data().calories
};
});
})
)
.subscribe((exercises: Exercise[]) => {
// code...
}, error => {
// code...
}));
当我尝试编译此代码时,出现以下错误:
ERROR in src/app/training/training.service.ts(41,44): error TS2339: Property 'name' does not exist on type '{}'.
src/app/training/training.service.ts(42,48): error TS2339: Property 'duration' does not exist on type '{}'.
src/app/training/training.service.ts(43,48): error TS2339: Property 'calories' does not exist on type '{}'.
我认为该语法可能与RxJS的早期版本相比已经过时,但是我似乎无法解决需要更改的内容。
答案 0 :(得分:0)
因此,我不得不对.pipe.map()中的代码稍作更改,以将数据返回为“ Exercise”,如下所示:
.pipe(
map(docArray => {
return docArray.map(doc => {
const data = doc.payload.doc.data() as Exercise;
const idExercise = doc.payload.doc.id;
return {idExercise, ...data};
});
})
)
答案 1 :(得分:0)
.pipe(
map(docArray => {
return docArray.map(doc => {
return {
id: doc.payload.doc.id,
...doc.payload.doc.data() as Exercise
};
});
})
)