Failed to navigate: Cannot read property 'lng' of undefined.ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'lng' of undefined


I would like to show the location map according to the location information from service.I get location information correctly.But I get an undefined error in html.Where do i make mistakes





      <sebm-google-map id="map"  [latitude]="" [longitude]="map.lng" 


import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';   
import {AgmCoreModule,SebmGoogleMap} from 'angular2-google-maps/core';
import {DataApi} from '../../app/shared/shared';

selector: 'page-map',
templateUrl: 'map.html',

 export class MapPage {

   map : any;
   constructor(public navCtrl: NavController, public navParams: 
   NavParams,private dataApi : DataApi) {

   ionViewDidLoad() {
    console.log('ionViewDidLoad MapPage');
    let games =;
    let tourneyData = this.dataApi.getCurrentTourney();
    let location = tourneyData.locations[games.locationId]; = {
    lat :  location.latitude,
    lng : location.longitude,
    zoom : 60,
    markerLabel : games.location



import {Injectable} from '@angular/core';
 import {Http,Response} from '@angular/http';

 import 'rxjs';
 import {Observable} from 'rxjs/Observable';
 export class DataApi {

 private url = '';   //
 currentTourney : any = {};
 private tourneyData = {};
 constructor(private http:Http){
    return new Promise(resolve =>{
      .subscribe(res => resolve(res.json()))

 /*   getAdress(): Promise<Team> {
     return this.http.get(this.url)
    .map(rsp => rsp.json())

  getTournamentData(tourneyId,forceRefresh : boolean = false) : Observable<any>{

    if(!forceRefresh && this.tourneyData[tourneyId]){
      this.currentTourney = this.tourneyData[tourneyId];
      console.log("no need to make Http call");
      return Observable.of(this.currentTourney);

    console.log("about to make Http call")
    return this.http.get(`${this.url}/tournaments-data/${tourneyId}.json`)
    .map((response:Response) =>{
        this.currentTourney = response.json();
        return this.currentTourney;

    return this.currentTourney;

    return this.getTournamentData(,true);



My guess would be that at the moment your map is created, and map.lng are still undefined. Make sure that the map is created only once you have a latitude and longitude to set. Because it probably fails if you’re trying to set it when and map.lng are not set yet.


your service is sending requests --> they are async and http is returning observables.

you can not simply do this:

let variable = myService.asyncStuff()

you need to wait until the request has finished:

this.dataApi.getCurrentTourney(data).subscribe(() => {
  // data is defined

Read about observables … please


A solution came from Stackoverflow,and my problem is solved.Thank you for your help.

      <sebm-google-map id="map"  [latitude]="" [longitude]="map.lng" 
      <sebm-google-map id="map"  [latitude]="map?.lat" [longitude]="map?.lng" 


But you should listen to @bengtler advise. Then you wouldn’t need the elvis operator. Do you even know why it fixed your issue?


No, I do not know. :smiley: Just solved my problem.I will consider your suggestion carefully.thank you


Please please do some reading before just pasting something from stackOverflow and celebrate if it works, because you become a better programmer when you truly understand what you’re doing :slight_smile: And better programmers make better apps.


Why is “?” working, I had the same problem, and ? did the work. Also I checked for @bengtler solution but doesn’t fixed the problem without elvis operator


I think the Elvis operator is an antipattern.

Part of the contract between template and controller should be that the controller is responsible for having all properties accessed by the template be in a sane state at all times. That is not the template’s job.