node.js - ionic 2 undefined is not an object 错误

标签 node.js cordova angular ionic2

我是 ionic 2 的新手。我遇到了一个问题。一旦我使用 map 定义 var map; 它就在 loadMap() 功能良好。但是,如果我像 map:any; 这样定义 map ,它就不起作用。抛出这样的错误。

Error: Uncaught (in promise): TypeError: undefined is not an object (evaluating 'this.map')

import { Component } from '@angular/core';
import { NavController,Platform } from 'ionic-angular';
import { Geolocation } from 'ionic-native';
import {DataService} from '../../providers/data-service';
declare var L:any;
@Component({
  selector: 'page-my-map',
  templateUrl: 'my-map.html',
  providers:[DataService]
})
export class MyMap {
      public data: any;

map:any=null;
markersLayer;

  myPositionLayer;
  myCircleLayer;
  showingon;
   LeafIconMe;
LeafIcon;


  constructor(platform: Platform, public dataService: DataService) {

      platform.ready().then(() => {

      Geolocation.getCurrentPosition().then((resp) => {
        });

    this.loadMap();
    this.loadData();

    });


  }

  loadMap(){

         this.map = L.map('map', {}).setView([40.980963, 29.0966243], 10);
L.tileLayer(tileURL, {
        maxZoom: 18,
        attribution: ''
    }).addTo(this.map);
     this.map.locate({ setView: true, maxZoom: 16 });


      this.dataService.getData()
        .then(data => {

          this.data = data;
         this.data.forEach(function(veri){
              console.log(veri.lat);
             L.marker([veri.lat, veri.lon]).addTo(this.map);

          })

        });
     console.log("map"+this.map);
  }
loadData(){
               console.log("data"+this.map);




    }


}

我的 npm 列表如下所示。

npm list --depth=0
msson@ /Users/baris/ionic2/denden/msson
├── @angular/common@2.0.0
├── @angular/compiler@2.0.0
├── @angular/compiler-cli@0.6.2
├── @angular/core@2.0.0
├── @angular/forms@2.0.0
├── @angular/http@2.0.0
├── @angular/platform-browser@2.0.0
├── @angular/platform-browser-dynamic@2.0.0
├── @angular/platform-server@2.0.0
├── @ionic/app-scripts@0.0.36
├── @ionic/storage@1.0.3
├── ionic-angular@2.0.0-rc.1
├── ionic-native@2.2.3
├── ionicons@3.0.0
├── rxjs@5.0.0-beta.12
├── typescript@2.0.3
└── zone.js@0.6.21

你能给我任何解决这个问题的建议吗?提前致谢..

最佳答案

this.data.forEach(function(veri){
    console.log(veri.lat);
    L.marker([veri.lat, veri.lon]).addTo(this.map);
})

您需要使用 arrow function (而不是 function)来保留 this context

this.data.forEach((veri) => {
    console.log(veri.lat);
    L.marker([veri.lat, veri.lon]).addTo(this.map);
})

关于node.js - ionic 2 undefined is not an object 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40239847/

相关文章:

javascript - 如何在 visual studio 2012 模拟器上调试 phonegap javascript 应用程序

angular - 带有 FormBuilder 的 Ionic 2 多个复选框

Angular NGX 图表 : using a custom legend

javascript - firebase 部署到自定义区域以执行预定功能

javascript - 解析node.js中的html内容

javascript - 动态创建表时的 JQuery Mobile 表回流问题

javascript - JS/TS必须使用bind但仍需要原来的this

javascript - 通过 node.js 运行带参数的 Bash 脚本

javascript - Node JS Inspect 显示属性具有值,但检查属性返回未定义

cordova - 如何从 fork 存储库安装cordova插件