属性 类型 {} 上不存在
Property doesn't exist on type {}
我正在尝试使用 snapshotChanges 从 firebase 数据库中获取对象列表。
Angular 版本: 7.2.0,
火力地堡版本:5.8.1,
RxJS 版本:6.3.3,
Angular火2: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 相比已经过时,但我似乎无法弄清楚我需要更改什么。
所以我不得不将 .pipe.map() 中的代码稍微更改为 return 数据作为 "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};
});
})
)
.pipe(
map(docArray => {
return docArray.map(doc => {
return {
id: doc.payload.doc.id,
...doc.payload.doc.data() as Exercise
};
});
})
)
我正在尝试使用 snapshotChanges 从 firebase 数据库中获取对象列表。
Angular 版本: 7.2.0, 火力地堡版本:5.8.1, RxJS 版本:6.3.3, Angular火2: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 相比已经过时,但我似乎无法弄清楚我需要更改什么。
所以我不得不将 .pipe.map() 中的代码稍微更改为 return 数据作为 "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};
});
})
)
.pipe(
map(docArray => {
return docArray.map(doc => {
return {
id: doc.payload.doc.id,
...doc.payload.doc.data() as Exercise
};
});
})
)