Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未捕获的TypeError:对象没有方法。。。Javascript_Javascript_Javascript Objects_Typeerror - Fatal编程技术网

未捕获的TypeError:对象没有方法。。。Javascript

未捕获的TypeError:对象没有方法。。。Javascript,javascript,javascript-objects,typeerror,Javascript,Javascript Objects,Typeerror,我有一个问题,我得到一个错误,说 “未捕获的TypeError:对象f771b328ab06没有方法'addLocation'” 我真的不知道是什么原因造成的。'f771b328ab06'是错误中的用户ID。我可以添加一个新用户并防止用户被复制,但是当我尝试将他们的位置添加到列表中时,会出现此错误 有人知道出了什么问题吗?初始化函数的else语句中也会出现错误(如果用户ID存在,只需追加位置,而不创建新用户)。我在代码中有一些注释,我很确定这部分是因为我修改了另一个用户提供的示例 functio

我有一个问题,我得到一个错误,说

“未捕获的TypeError:对象f771b328ab06没有方法'addLocation'”

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

有人知道出了什么问题吗?初始化函数的
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(生成编号的部分位于上面带有注释的代码块中):

之后,将ID作为参数传递:

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

如果有人有任何建议,我将不胜感激。谢谢

在这里,您可以通过以下方式获取用户id:

var user_id = data[0];
所以这是json答案的一部分:可能是字符串或另一个单词,这不能是用户对象。您应该尝试通过以下方式更新“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);

嗯,我是JS新手,我只是想能够搜索密钥以防止ID被复制。我把“1”作为占位符放在那里。有更好的方法吗?我已经找到了。我没有在if语句中定义用户。哎呀。我注释掉了
用户。用户[user\u id]=“1”部分,它似乎工作正常。感谢您的洞察力。如果您是新的签出Firebug,它可能会真正帮助您调试代码。
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);