[英]Type 'Subscription' is not assignable to type 'Observable<Exercise[]>'
Angular CLI: 6.0.8 角CLI:6.0.8
Node: 8.11.3 节点:8.11.3
I just learned Udemy - Angular (Full App) with Angular Material, Angularfire & NgRx. 我刚刚通过Angular Material,Angularfire和NgRx学习了Udemy-Angular(完整应用程序)。 Up on lesson 72 Listening to Value Changes (of_Firestore) I have an error 在第72课上,聆听值更改(of_Firestore)我有一个错误
export class NewTrainingComponent implements OnInit { exercises: Observable<Exercise[]>; constructor( private trainingService: TrainingService, private db: AngularFirestore) { } ngOnInit() { this.exercises = this.db .collection('availableExercises') .snapshotChanges() .pipe( map(docArray => { return docArray.map(doc => { return { id: doc.payload.doc.id, name: doc.payload.doc.data().name, duration: doc.payload.doc.data().duration, calories: doc.payload.doc.data().calories }; }); }) ).subscribe(result => { console.log(result); }); } onStartTraining(form: NgForm) { this.trainingService.startExercise(form.value.exercise); } }
error TS2322: Type 'Subscription' is not assignable to type 'Observable'. 错误TS2322:无法将“预订”类型分配给“可观察”类型。
error TS2339: Property 'name' does not exist on type '{}'. 错误TS2339:类型“ {}”上不存在属性“名称”。
error TS2339: Property 'duration' does not exist on type '{}'. 错误TS2339:类型“ {}”上不存在属性“持续时间”。
error TS2339: Property 'calories' does not exist on type '{}'. 错误TS2339:类型“ {}”上不存在属性“卡路里”。
The error message is very obvious. 该错误信息非常明显。 The exercises
is a type of Observable
as you have defined: exercises
是您定义的Observable
的类型:
exercises: Observable<Exercise[]>;
But you assign it to a subscription in your ngOnInit()
method. 但是您可以通过ngOnInit()
方法将其分配给预订。
Your ngOnInit()
method should be like the following (do not make the assignment to exercises
): 您的ngOnInit()
方法应类似于以下内容(不要将其分配给exercises
):
ngOnInit() {
this.db
.collection('availableExercises')
.snapshotChanges()
.pipe(
map(docArray => {
return docArray.map(doc => {
return {
id: doc.payload.doc.id,
name: doc.payload.doc.data().name,
duration: doc.payload.doc.data().duration,
calories: doc.payload.doc.data().calories
};
});
})
).subscribe(result => {
console.log(result);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.