Search code examples
jsonwindowstypescriptionic-frameworkgeolocation

Property 'locations' does not exist on type 'Object'


import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';


@Injectable()
export class LocationsProvider {

  data: any;

  constructor(public http: HttpClient) {

  }

  load() {

    if (this.data) {
     return Promise.resolve(this.data);
    }

    return new Promise(resolve => {

      this.http.get('assets/data/locations.json').subscribe(data => {

        this.data = this.applyHaversine(data.locations);

        this.data.sort((locationA, locationB) => {
          return locationA.distance - locationB.distance;
        });

        resolve(this.data);
      });

    });

  }

enter image description here

i am pretty new here, and pretty new to ionic, i'll probably requires detailed solution, i cant seems to make ionic read a json file


Solution

  • enter image description here

    You are getting a compile time error in data.locations specifically locations is not defined on the data property.

    Fix

    Tell TypeScript that it is e.g. use an assertion:

      this.data = this.applyHaversine((data as any).locations);