未捕获的类型错误:对象没有方法..Javascript

Uncaught TypeError: Object has no method ... Javascript

本文关键字:对象 有方法 Javascript 错误 类型      更新时间:2023-09-26

我遇到了一个问题,我得到了一个错误,上面写着。。。

"未捕获的类型错误:对象f771b328ab06没有方法'addLocation'"

我真的不确定是什么原因造成的。'f771b328ab06'是错误中的用户ID。我可以添加一个新用户并防止用户被复制,但当我试图将他们的位置添加到列表中时,我会收到这个错误。

有人看到出了什么问题吗?该错误也发生在initialize函数的else语句中(如果用户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,不需要在这里生成(只需要接收),所以我注释掉了创建随机ID的部分。在执行此操作时,我必须在Users()中的createUser方法中添加一个参数,以便我可以将ID作为参数从Initialize()传递。参见以下对createUser的更改:

之前,使用生成的ID(生成编号的部分在上面带有注释的代码块中):

this.createUser = function() {
    var id = this.generateId();
    this.users[id] = new User(id);
    return this.users[id];
};

之后,ID作为参数传递:

this.createUser = function(id) { 
    this.users[id] = new User(id);
    return this.users[id];
};

如果有人有任何建议,我将非常感谢。谢谢!

您可以通过以下方式获取user_id:

var user_id = data[0];

所以它是json答案的一部分:可能是一个字符串或另一个dictionary,它不能是用户对象。你应该尝试在"if"块内的成功函数中更新你的代码:

user = users.createUser(user_id);
//The following line is a non sense for me you put an int inside 
//an internal structure of your class that should contain object
//users.users[user_id] = "1"; 
user.addLocation(latitude, longitude);