F
F
Felino2019-12-14 19:53:50
Angular
Felino, 2019-12-14 19:53:50

What's wrong? Where is the mistake?

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, of} from 'rxjs';
import {Task1Module} from '../module/task1.module';
import {catchError, tap} from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class Task1Service {

  connect="../assets/Task1.json";

  constructor(private http:HttpClient) {  }

  private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
      console.error(error);
      return of(result as T);
    };
  }

  GetData():Observable<Task1Module[]>{
    return this.http.get<Task1Module[]>(this.connect).pipe(
      tap((obj)=>console.log(obj)),
      catchError(this.handleError('get data',[]))
    );
  }

}

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {Observable} from 'rxjs';



@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class Task1Module {
  constructor(
    public distance:Observable<distance>,
    public convert_to:string
  ) {
  }
}
export class distance{
  constructor(
    public unit:string,
    public value: number
  ) {
  }
}

The error is here!
Error:(27, 24) TS2339: Property 'unit' does not exist on type 'Observable'
Error:(29, 13) TS2322: Type 'number' is not assignable to type 'string'.
import {Component, OnInit} from '@angular/core';
import {Task1Service} from '../service/task1.service';
import {distance, Task1Module} from '../module/task1.module';

@Component({
  selector: 'app-task1',
  templateUrl: './task1.component.html',
  styleUrls: ['./task1.component.css']
})
export class Task1Component implements OnInit {

  data: Task1Module[] = [];
  i = 0;
  data1: distance[] = [];

  constructor(private http: Task1Service) {
  }


  ngOnInit() {
    this.http.GetData().subscribe((obj) => {
      this.data = obj;


      for (let a of this.data) {
        // Переводим сантиметры
        if (a.distance.unit == 'cm') {
          if (a.convert_to == 'm') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value * 100);
          }
          if (a.convert_to == 'in') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value / 10 / 25.4);
          }
          if (a.convert_to == 'ft') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value / 10 / 304.8);
          }
        }
        // Переводим дюймы
        if (a.distance.unit == 'in') {
          if (a.convert_to == 'm') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value * 25.4 / 10 / 100);
          }
          if (a.convert_to == 'cm') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value * 25.4 / 10);
          }
          if (a.convert_to == 'ft') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value * 25.4 / 304.8);
          }
        }
        // Переводим футы
        if (a.distance.unit == 'ft') {
          if (a.convert_to == 'm') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value * 304.8 / 10 / 100);
          }
          if (a.convert_to == 'cm') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value * 304.8 / 10);
          }
          if (a.convert_to == 'in') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value * 304.8 / 25.4);
          }
        }
        // Переводим метры
        if (a.distance.unit == 'm') {
          if (a.convert_to == 'ft') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value / 100 / 10 / 304.8);
          }
          if (a.convert_to == 'cm') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value / 100);
          }
          if (a.convert_to == 'in') {
            this.data1[this.i] = new distance(a.convert_to, a.distance.value / 100 / 10 / 25.4);
          }
        }
        this.i = this.i + 1;
      }
      console.log(this.data1);
    });
  }

}

[
  {
    "distance": {
      "unit": "m",
      "value": "1"
    },
    "convert_to": "cm"
  },
  {
    "distance": {
      "unit": "cm",
      "value": "1"
    },
    "convert_to": "km"
  },
  {
    "distance": {
      "unit": "cm",
      "value": "1"
    },
    "convert_to": "mm"
  }
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-12-14
@Xuxicheta

because

export class Task1Module {
  constructor(
    public distance:Observable<distance>,
    public convert_to:string
  ) {
  }
}

the distance field is an Observable, and it does not have a unit property,
and then you try to assign a number to convert_out.value, but it is a string.
Write everything humanly and this will be immediately visible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question