TypeError: undefined不是一个函数——需要排除故障
TypeError: undefined is not a function --- Need to troubleshoot
我得到了这个非常奇怪的错误
TypeError: undefined is not a function
at h.$scope.getCoordDistance (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:32464:21)
at http://run.plnkr.co/NDFxGL6q55m1601d/script.js:32491:14
at Array.forEach (native)
at Object.q [as forEach] (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:280)
at new <anonymous> (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:32480:11)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:34:479)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:35:103)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:66:467
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:53:250
at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:386)
在网上搜索了一个半小时后,我完全不知道这是什么意思。有人知道这是什么意思吗?下面是我的代码:
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
var myLat,myLon,locLat,locLon;
$scope.ASiteLocs = [{
"name": "IL5077 BRUSSELS",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.58543899999999,38.955472,0"
}
}, {
"name": "IL5076 KAMPSVILLE",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.661923,39.29403,0"
}
}, {
"name": "IL5146 CARROLLTON",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.39965700000001,39.309142,0"
}
}];//there are a couple thousand of these
$scope.SSiteLocs = [/*More Locations*/];// but its too long
$scope.SiteLocs = $scope.SSiteLocs.concat($scope.ASiteLocs);
repoSortOrder = "site.name";
navigator.geolocation.getCurrentPosition(GetLocation);
function GetLocation(location, myLat, myLon) {
$scope.myLat = location.coords.latitude;
$scope.myLon = location.coords.longitude;
} $scope.getCoordDistance = function(myLat, myLon,locLat, locLon) {
var lat2 = $scope.myLat;
var lon2 = $scope.myLon;
var lat1 = $scope.locLat;
var lon1 = $scope.locLon;
var R = 3959; // Mean Earth radius in miles
var x1 = lat2 - lat1;
var dLat = x1.toRad();
var x2 = lon2 - lon1;
var dLon = x2.toRad();
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
$scope.d = R * c;
}
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
angular.forEach($scope.SSiteLocs, function(object) {
object.carrier = 'Sprint';
})
angular.forEach($scope.ASiteLocs, function(object) {
object.carrier = 'AT&T';
});
angular.forEach($scope.SiteLocs, function(location, locLat, locLon) {
var clength = location.Point.coordinates.length;
if (location.Point.coordinates.substring(clength - 2, clength) === ",0") {
location.Point.coordinates = location.Point.coordinates.substring(0, clength - 2).split(",");
Lat = location.Point.coordinates[0];
Lon = location.Point.coordinates[1];
Com = ",";
location.Point.coordinates = Lon.concat(Com, Lat);
}
$scope.locLat = location.Point.coordinates[0];
$scope.locLon = location.Point.coordinates[1];
$scope.getCoordDistance();
location.distance = d;
});
});
如果我尝试使用parseInt();
解析myLat, myLon, locLat, locLon
,我会得到这个错误:
TypeError: Cannot read property 'toRad' of undefined
at h.$scope.getCoordDistance (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:46:45)
at http://run.plnkr.co/NDFxGL6q55m1601d/script.js:73:14
at Array.forEach (native)
at Object.q [as forEach] (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:280)
at new <anonymous> (http://run.plnkr.co/NDFxGL6q55m1601d/script.js:62:11)
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:34:479)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:35:103)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:66:467
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:53:250
at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js:7:386)
任何帮助都非常感谢!
这是一个项目:http://plnkr.co/edit/nRQc7Ym0lsaK6jQwd626?p=preview
在您甚至从navigator.geolocation.getCurrentPosition()
获得坐标之前运行getCoordDistance()
(顺便说一句,这是一个异步调用)。这意味着$scope.myLat
和$scope.myLon
都是null/undefined,这会导致错误"Cannot read property 'toRad' of undefined"。
检查这个活塞
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 我可以在json对象中添加一个函数吗
- Javascript使函数一个接一个地执行
- 是JavaScript中的函数一个对象
- 两个几乎相等的jQuery函数;一个适用于IE,一个不适用于IE
- 两个Javascript函数一个window.onload=Custom.init;和一个window.onload=f
- 使javascript加载函数一个接一个地执行
- 如何通过两个嵌套的匿名函数(一个带有超时的事件处理程序)传递变量
- 为什么我不能让两个jQuery函数一个在另一个里面呢?
- 给函数一个变量名,而不是它的值
- 如何使用嵌入函数(一个滚动页由Pete - peachananr)
- 是否有可能给JavaScript函数一个类型/类?
- 确保两个函数一个接一个地执行,其中第一个函数内部有一个异步调用
- 两个不同的ajax函数一个接一个调用返回相同的值
- jQuery第一次更改函数一个工作
- 给这个函数一个id
- 如何给回调函数一个值一个变量在特定时刻
- 使用回调使两个函数一个接一个地运行
- 两个函数(一个用php)是否可以用“;onclick”;
- 两个几乎相同的函数.一个函数用Queue进行排队.Jquery效果在queued函数上不起作用.为什么