내가 정말이 원인을 모르겠어요 내가 말한다 오류가 발생하는 문제 ...catch되지 않은 형식 오류 : 개체 ... 자바 스크립트
"Uncaught TypeError: Object f771b328ab06 has no method 'addLocation'"
에 봉착를있는 방법이 없습니다. 'f771b328ab06'
은 오류의 사용자 ID입니다. 새 사용자를 추가하고 사용자가 중복되는 것을 방지 할 수 있지만 목록에 위치를 추가하려고하면이 오류가 발생합니다.
누가 잘못되었는지보고있는 사람이 있습니까? 이 오류는 초기화 함수의 else
문에서도 발생합니다 (사용자 ID가있는 경우 위치를 추가하고 새 사용자를 만들지 마십시오). 코드에 몇 가지 메모가 있는데, 다른 사용자가 제공 한 예제를 어떻게 수정했는지 부분적으로는 확신 할 수 있습니다. 내가 전화에서 ID를 얻고 (만 수신) 여기를 생성 할 필요가 없기 때문에
function User(id) {
this.id = id;
this.locations = [];
this.getId = function() {
return this.id;
};
this.addLocation = function(latitude, longitude) {
this.locations[this.locations.length] = new google.maps.LatLng(latitude, longitude);
alert("User ID:");
};
this.lastLocation = function() {
return this.locations[this.locations.length - 1];
};
this.removeLastLocation = function() {
return this.locations.pop();
};
}
function Users() {
this.users = {};
//this.generateId = function() { //I have omitted this section since I send
//return Math.random(); //an ID from the Android app. This is part of
//}; //the problem.
this.createUser = function(id) {
this.users[id] = new User(id);
return this.users[id];
};
this.getUser = function(id) {
return this.users[id];
};
this.removeUser = function(id) {
var user = this.getUser(id);
delete this.users[id];
return user;
};
}
var users = new Users();
function initialize() {
alert("Start");
$.ajax({
url: 'api.php',
dataType: 'json',
success: function(data){
var user_id = data[0];
var latitude = data[1];
var longitude = data[2];
if (typeof users.users[user_id] === 'undefined') {
users.createUser(user_id);
users.users[user_id] = "1";
user_id.addLocation(latitude, longitude); // this is where the error occurs
}
else {
user_id.addLocation(latitude, longitude); //here too
alert(latitude);
}
}
})
}
setInterval(initialize, 1000);
, 나는 임의의 ID를 생성하는 부분에서 댓글을 달았습니다. 이렇게하면 Initialize()
에서 ID를 전달할 수 있도록 createUser
메서드에 매개 변수를 Users()
내에 추가해야했습니다. 로 전달되는 ID와 후
this.createUser = function() {
var id = this.generateId();
this.users[id] = new User(id);
return this.users[id];
};
: 생성 된 ID와
전 (개수 주석 상기 코드 블록에 생성되는 부분) 아래 createUser
에 변경보기 인수 :
this.createUser = function(id) {
this.users[id] = new User(id);
return this.users[id];
};
누구든지 제안을하면 정말 고맙겠습니다. 감사! 여기
좋고 그냥 중복되는 ID를 방지하기 위해 키를 검색 할 수 있기를 원 : 당신은 "만약"블록 내부에 성공 함수에서 코드를 업데이트하려고합니다 . 거기에 자리 표시 자로 "1"을 넣었습니다. 이 작업을 수행하는 더 좋은 방법이 있습니까? – mkyong
알아 냈습니다. if 문에는 사용자 정의가 없습니다. 죄송합니다. 나는'users.users [user_id] = "1";'섹션을 주석 처리했으며 제대로 작동하는 것으로 보입니다. 통찰력에 감사드립니다. – mkyong
새로운 체크 아웃 방화범이라면 코드를 디버그하는 데 정말로 도움이 될 것입니다. – AsTeR