mkyong
mkyong

Reputation: 12947

Uncaught TypeError: Object has no method ... Javascript

I'm having an issue where I get an error that says...

"Uncaught TypeError: Object f771b328ab06 has no method 'addLocation'"

I'm really not sure what's causing this. The 'f771b328ab06' is a user ID in the error. I can add a new user and prevent users from being duplicated, but when I try to add their location to the list, I get this error.

Does anybody see what's going wrong? The error occurs in the else statement of the initialize function as well (if the user ID exists, just append the location and do not create a new user). I have some notes in the code, and I'm pretty sure that this is partly due to how I have modified an example provided by another user.

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);

Since I get the ID from the phone and do not need to generate it here (only receive it), I commented out the part that creates the random ID. In doing this, I had to add a parameter to the createUser method within Users() so that I can pass the ID as an argument from Initialize(). See the changes to createUser below:

Before, with the generated ID (the part where the number is generated is in the above code block with comments):

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

After, with the ID passed as an argument:

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

If anyone has any suggestions I would really appreciate it. Thanks!

Upvotes: 0

Views: 3858

Answers (1)

AsTeR
AsTeR

Reputation: 7541

Here you're getting user_id by :

var user_id = data[0];

So it's a part of the json answer : maybe a string or another dictionnary, this can't be a user object. You should try to update your code in your success function inside the "if" block by :

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);

Upvotes: 2

Related Questions