gpt4 book ai didi

javascript - 类型错误 : undefined is not a function --- Need to troubleshoot

转载 作者:行者123 更新时间:2023-12-03 12:03:06 24 4
gpt4 key购买 nike

我收到这个非常奇怪的错误

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,这会导致错误“无法读取属性 'toRad' of未定义”。

检查这个plunker

关于javascript - 类型错误 : undefined is not a function --- Need to troubleshoot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25311780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com