Property 'data' does not exist on type 'any[]'

I have created a dummy service to return some dummy data, then get it called by a component ts file. But i got below error.

Found a few posts with similar error,but still can not figure out how to correct mine.

error:

Error: src/app/roaster-load/file-load-init/file-load-init.component.ts:60:19 – error TS2339: Property ‘data’ does not exist on type ‘any[]’.

file-load-init.component.ts:

`

import { NgbModal, ModalDismissReasons, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';
import { BaseComponent } from '../../common/scripts/baseComponent';
import { WINDOW } from '../../common/scripts/window.service';
import { Component, OnInit} from '@angular/core';
import { HttpClientModule, HttpClient, HttpHandler } from '@angular/common/http';
import { UploadService } from '../UploadService/upload.service';
import { InvalidSOEIDModel } from '../Uploadmodel/uploadmodel';
import { Subscription } from 'rxjs';

@Component({
  selector: 'app-file-load-init',
  templateUrl: './file-load-init.component.html',
  styleUrls: ['./file-load-init.component.css']
})
export class FileLoadInitComponent implements OnInit {
  
  InvalidSOEIDCount: any;     

  InvalidSOEIDModel:InvalidSOEIDModel[];
  ngOnInit(): void {
  }

  constructor(private http: HttpClient,
              private UploadService: UploadService,) {}

  OnUpload(){

  this.GetSOEIDVerifyData();
  };


  private   GetSOEIDVerifyData(){  
    this.UploadService.UploadDataReturn()
     .subscribe((res: any[]) => {
          console.log(res);
          if (res.data) {
            this.InvalidSOEIDCount = res.data
          }  
        }
     )}
}

upload.service.ts:

 import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs';
    import { InvalidSOEIDModel } from '../Uploadmodel/uploadmodel'
    import {of} from 'rxjs';
    
    @Injectable()
export class UploadService {

 UploadDataReturn(): Observable<InvalidSOEIDModel[]> {
  return of (  [
      {
        SOEID: "AAAAA"
      },
      {
        SOEID: "BBBBB"
      }])
}
}
    

     

>Solution :

private GetSOEIDVerifyData(){  
        this.UploadService.UploadDataReturn()
         .subscribe((res: InvalidSOEIDModel[]) => { 
//Replaced any with InvalidSOEIDModel
//You can just write res as well it work , no need to mention type
//Your response is a array of InvalidSOEIDModel so response does't have 
// any property of data but by writing res.data you are trying to acces
//property that  does not exist
              if (res.data) {
                this.InvalidSOEIDCount = res.data //This is wrong
                this.InvalidSOEIDCount = res.length // This is correct 
              }  
            }
         )}

Leave a Reply