简体   繁体   中英

Property does not exist on type []

i try to compile my PWA and on localhost on ionic-serve evrything is workin, but whne i try to compile i got a type error on dev = []. Here is my code on device.page.ts

import { Component, OnInit } from '@angular/core';
import { MenuController } from '@ionic/angular';
import { Storage } from '@ionic/storage-angular';
import { ApiService } from '../api.service';
import { Location } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { HttpClient, HttpHeaders } from '@angular/common/http';




@Component({
  selector: 'app-device',
  templateUrl: './device.page.html',
  styleUrls: ['./device.page.scss'],
})
export class DevicePage implements OnInit {
  id: any;
  dev = [];
  data: any;
  parse: any;
  constructor(private activatedRoute:ActivatedRoute, public http:HttpClient, private storage: Storage, private api: ApiService, public menuCtrl: MenuController,
    ) {
    this.id = this.activatedRoute.snapshot.paramMap.get('id');  
  }


  ngOnInit() {
  }

  ionViewDidEnter() {
    this.api.getDeviceInfo(this.id).subscribe((response) => {
      console.log(response);
      this.dev = response;
    })
  }

  deleteDevice(id){
    alert(id);
  }

  ionViewWillEnter() {
    this.menuCtrl.enable(true);
  }
}

here is the function in api service:

    getDeviceInfo(id): Observable<Device[]> {
    return this.http.get<Device[]>(`${this.apiURL}/get-devices-info?id=` + id)
      .pipe(
        tap(device => console.log('Device retrieved!')),
        catchError(this.handleError<Device[]>('Get user', []))
      );
  }

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

and here is my page:

    <ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    
    <ion-title>{{ dev.name }} </ion-title>
    <ion-button  (click)='deleteDevice(dev.id)'>Elimina</ion-button>
  </ion-toolbar>
</ion-header>

<ion-content>
  <img src="/assets/images/{{ dev.img }}" style="width: 50%" >
  <ion-list>
    <ion-item>
      <ion-label>
        Stati e Messaggi dispositivo
      </ion-label>
    </ion-item>
    <ion-item  *ngFor="let message of dev.messages">
      <ion-label class="ion-text-wrap">
        <ion-text color="primary">
          <h3>{{ message.created_at}}</h3>
        </ion-text>
        <p>{{ message.data }}</p>
        <ion-text color="secondary">
          <p>{{ message.sequence }}</p>
        </ion-text>
      </ion-label>
    </ion-item>
  </ion-list>
</ion-content>

It is working on localhost. When i try to compile the i got error:

Error: src/app/device/device.page.html:21:43 - error TS2339: Property 'messages' does not exist on type '{}'.

Can someone help me to understand?

Your dev class property has been initialized as an empty array = [] , you are trying to access a messages property from the dev array...

Also, I strongly recommend (for your own benefit) to stop using any and start creating your own data types, otherwise you will end up having a vanilla javascript project in a typescript one...

It is better, using interfaces, i know. But for those who stills stuck on this i find the error: you need to use dev['name'] instead of dev.name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM