简体   繁体   English

无法将“订阅”类型分配给“可观察”类型 <Exercise[]> “

[英]Type 'Subscription' is not assignable to type 'Observable<Exercise[]>'

I use 我用

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.

相关问题 在angular2的服务层上修改对象:无法将订阅分配给可观察的类型 - Modifying object at service layer in angular2: Subscription is not assignable to the type observable 输入&#39;可观察的<Object> &#39; 不可分配给类型 &#39;Observable<IUser[]> &#39; - Type 'Observable<Object>' is not assignable to type 'Observable<IUser[]>' RXJS可观察的枚举类型,类型为“可观察” <Type> &#39;不可分配给&#39;Type&#39;类型 - RXJS Observable enum type, Type 'Observable<Type>' is not assignable to type 'Type' 不可分配给“可观察”类型 <task> &#39;。角4 - is not assignable to type 'Observable<task>'.Angular 4 可观察的 <void | AuthError> &#39;不可分配给&#39;Observable类型 <Action> - Observable<void | AuthError>' is not assignable to type 'Observable<Action> Angular2:类型'订阅'不能分配给类型 - Angular2 : Type 'Subscription' is not assignable to type “订阅”类型的参数不可分配给类型的参数 - Argument of type 'Subscription' is not assignable to parameter of type Angular 订阅不可分配给可观察的 - Angular subscription is not assignable to observable 类型“可观察” <boolean | “”> &#39;不可分配给&#39;Observable类型 <boolean> &#39;TS2322 - Type 'Observable<boolean | “”>' is not assignable to type 'Observable<boolean>' TS2322 输入&#39;可观察的<HttpEvent<T> &gt;&#39; 不可分配到类型 &#39;Observable<T> &#39; - Type 'Observable<HttpEvent<T>>' is not assignable to type 'Observable<T>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM